Search code examples
sassas-macro

Scan values from a macro variable and store it in a column in data set using SAS


%let abc = ("234.34", "C56.67", "2345.67", "C67.56") ;

Output: Table1

Col1

234.34

C56.67

2345.67

C67.56

This is throwing an error, can somebody please guide me:

%macro generate ;


    %local i ;

    data table1;

    length Col1 $100.;

    %do i=1 %to %sysfunc(countw(&abc.));

          Col1 = %scan(&abc., &i.,,"sq");
          output;

     %end;
    run;

%mend;

%generate;

Solution

  • You can do this with a DO loop, but you will need to remove the () from the value of the macro variable.

    %let abc = ("234.34", "C56.67", "2345.67", "C67.56") ;
    data table1;
      length Col1 $100.;
      do Col1 = %substr(&abc,2,%length(&abc)-2);
        output;
      end;
    run;
    

    If the values do not contain () then you could also just use %scan(&abc,1,()), which also has the advantage of working whether or not the original value has the enclosing ().

    Or just remove the () from the value and add them back when you use the macro variable in place where they are needed.

    %let abc = "234.34", "C56.67", "2345.67", "C67.56" ;
    ...
      do Col1 = &abc ;
    ...
      where dxcode in (&abc) 
    ....