Search code examples
sassas-macro

Use variable values as the value for a macro variable


I have one SAS dataset that has a column with values that I want to use as a macro variable. Say the SAS dataset looks like this with one column:

varname
34
367
399
87

What I want is for all of the values in the column to become a concatenated string in a macro variable (suitable for using in an if statement with the in operator). The result would be equivalent to this:

%let var = %str('34','367','399','87');

How would I accomplish this?


Solution

  • I would use PROC SQL, but there are other ways:

    proc sql noprint;
    select "'" || varname || "'"
       into :var separated by ','
       from have;
    quit;
    
    %put var: &var;
    

    This will concatenate the variable values with a ' on either side. The separated by piece will put a , between all the values. :var tells SQL to save the results in a macro named var;