0
votes

I've used call symputx to create a list of macro variables Item 1 to Item N and now I want to transfer them to an array in another datastep so that spot 1 in the array gets Item1, spot 2 gets Item2, etc.

    do j=1 to &num_OR;
    rulesUsed{j}=&&Item&j;
    end;

I read that the double ampersand syntax is the way to reference macro variables like this but I keep getting all sorts of errors. I'm sure there's a simple way around this but I'm new to SAS and no document I've read through that's come up in searches mentions this exact type of problem.

2
Why are you using macro variables? You should stick with learning SAS first before trying to use the macro processor to generate SAS code for you. If you used call symputx() then you must have already had the data in a dataset, just use that dataset again.Tom
@Tom I need to create new columns in the dataset in order to apply some rules. The number of columns required will vary each time the program runs and therefore need to hold some naming convention to be looped through which is why I tried using arrays. Not sure how to assign an array to just the value of one row which is why I'm trying to make a list of macro variables then have the arrays reference them. --Basically if there's 5 things I need to add 5 columns to the dataset and column 1 has the value of thing 1, column 2 the value of thing 2, etc.S420L
@S420L What Tom's saying is that you shouldn't use macro variables for this. Arrays are fine - quite sensible - but the macro variables are not the right place for this. Merge the dataset that has this information using MERGE and then use an array, or something else. Feel free to ask a question with more of the specifics, if you'd like suggestions for how to approach it. Preferably with an example of the raw data and the wanted result.Joe

2 Answers

1
votes

The short answer is: don't do this, in general. Macro variables aren't a great way to store data, and there's nearly always a better way.

But if you need to, your issue here is that the macro variable can't use the data step variable.

 do j=1 to &num_OR;
    rulesUsed{j}=&&Item&j;
 end;

j is a data step variable, not a macro variable, and so it's not &j. You need to either:

1 - Use symget to retrieve the macro variable. That's a data step function that takes a normal data step character argument (so a variable, a " " string, etc.) and returns the macro variable with that name. So

rulesUsed[j] = symget(cats("item",j));

2 - Use a macro loop to retrieve the macro variable.

%do j = 1 %to &num_or;
  rulesUsed[&j.] = &&item&j;
%end;

Either of these methods work fine.

0
votes

If you have a dataset like below:

data have ;
  ruleno+1;
  input rule $20. ;
cards;
Value1
Value2
Value3
;

You can convert it to wide using PROC TRANSPOSE.

proc transpose data=have out=want(drop=_name_) prefix=rulesUsed ;
  var rule;
  id ruleno;
run;

enter image description here