1

I have a macro variable which stores a string of names, for example:

%let operation = add subtract divide multiply;

I wanted to transpose each element (to appear as observation) in the macro into a data set variable. So the data set should look like:

<obs> <operation>
<1>   add
<2>   subtract
<3>   divide
<4>   multiply

2 Answers 2

4

Use the SCAN() function. The default delimiters will work for your example, otherwise you can specify the exact delimiters to use.

%let operation= add subtract divide multiply;
data want ;
  length obs 8 operation $20 ;
  do obs=1 by 1 until (operation=' ');
    operation=scan("&operation",obs);
    if operation ne ' ' then output;
  end;
run;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response, to build on my question. Suppose I wanted to set an array to point to operations would I do it the following way: set want(keep=operation); array pointer(*) operation;
Are the words in macro variable operation referring to variable names in a SAS data set?
yes they are. I was basically treating this as an "arrays" exercise.
If OPERATION contains a list of variable names then use &OPERAtION where ever you would use a list of variable names. set want(keep=&operation); array op &operation; do i=1 to dim(op); ...
Nice loop structure, I like that better than the do while alternative. Seems shorter/cleaner.
2

I still don't know enough about what you have and what you want. This example is contrived but may give you some help regarding syntax etc.

%let operation = add subtract multiply divide;

data operation;
   length &operation 8;
   array operation[*] &operation (2 3 10 4);
   put 'NOTE: ' (operation[*])(=);
   run;

*data set of names;
proc transpose data=operation(obs=0) out=names name=operation;
   var &operation;
   run;
proc print;
   run;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.