3

How can I convert the output of a SAS data column into a macro variable?

For example:

Var1 | Var2
-----------
  A  |  1
  B  |  2
  C  |  3
  D  |  4
  E  |  5

What if I want a macro variable containing all of the values in Var1 to use in a PROC REG or other procedure? How can I extract that column into a variable which can be used in other PROCS?

In other words, I would want to generate the equivalent statement:

%LET Var1 =

  A
  B
  C
  D
  E
  ;

But I will have different results coming from a previous procedure so I can't just do a '%LET'. I have been exploring SYMPUT and SYMGET, but they seem to apply only to single observations.

Thank you.

Joe
  • 62,789
  • 6
  • 49
  • 67
vdiddy
  • 85
  • 1
  • 9

2 Answers2

6
proc sql;
  select var1 
    into :varlist separated by ' '
    from have;
quit;

creates &varlist. macro variable, separated by the separation character. If you don't specify a separation character it creates a variable with the last row's value only.

There are a lot of other ways, but this is the simplest. CALL SYMPUTX for example will do the same thing, except it's complicated to get it to pull all rows into one.

Joe
  • 62,789
  • 6
  • 49
  • 67
0

You can use it in a proc directly, no need for a macro variable. I used numeric values for your var1 for simplicity, but you get the idea.

data test;
input var1  var2 @@;
datalines;
1 100 2 200 3 300 4 400 5 500
run;



proc reg data=TEST;
MODEL VAR1 = VAR2;
RUN;
Victor
  • 16,609
  • 71
  • 229
  • 409