Search code examples
sassas-macro

Make a SAS data column into a Macro variable?


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.


Solution

  • 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.