Search code examples
sassas-macro

Create a column in SAS using a macro variable


I am trying to create a column using the string value of a macro variable in SAS. I have a dataset called want7 which has a column called 'ID'. I want to create a new dataset called want8 with a new column called 'ID1' by dynamically linking it to &string1 (as in the name of the column is linked to &string1) but the values of the column should equal the value of the 'ID' column in want7. How do I do this? Thanks in advance. I have only copied and pasted what I could write since I am relatively new to SAS.

 %let string1 = ID1;
 
 data want8; set want7;
 /*Something like &string1 = ID*
 run;

Solution

  • Using sashelp.class as an example (because it exists by default). Substitute as needed:

     %let string1 = ID1;
     
     data want8; 
        set sashelp.class;
            &string1 = age ;
     run;
    

    This will reread the dataset. If you just want renames, look at the dataset option rename=. See SAS documentation: https://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000695119.htm