How to Add a macro variable list as a column to a SAS dataset

549 views Asked by At

I have a macro variable, Nums, which has a list of values i.e 101, 102, 103, 104, 105. I have a SAS dataset which has various columns, one of which is a date column. I need to add the Nums macro variable to this SAS dataset as a column. Below is the code I have used to generate a macro variable list. Not sure how to proceed further.

PROC SQL;
SELECT ID
INTO IDs seperated by ',' FROM Datatable; 
QUIT;
1

There are 1 answers

2
Tom On

You can just use a DO statement.

So if you have macro variable NUMS with value like:

%let nums=101,102,103,104,105 ;

Then you can use it to generate part of a DO statement. Inside the DO loop you can set the old dataset. Make sure to OUTPUT the observations since you are writing more than one observation per iteration of the data step.

data want;
  do id=&nums ;
    set have;
    output;
  end;
run;

But if you have the data in a dataset already there is no need to first convert the numbers into strings so that they can be stored in a macro variable.

data want;
  set have;
  set datatable (keep=id) ;
run;