Search code examples
sassas-macro

How can I store a macro variable value into SAS dataset?


I need to store ​​macro variable values ​​in the SAS dataset like this:

PROC SQL;
    SELECT SUM(Invoice)/1000 FORMAT COMMAX18.2 INTO :val01 FROM SASHELP.CARS;
QUIT;
%PUT &val01.;

PROC SQL;
    SELECT COUNT(Model) FORMAT COMMAX12. INTO :val02 FROM SASHELP.CARS;
QUIT;
%PUT &val02.;

PROC SQL;
    SELECT SUM(MSRP)/1000000 FORMAT COMMAX18.2 INTO :val03 FROM SASHELP.CARS;
QUIT;
%PUT &val03.;

data WORK.teste;
   input col01 $ col02 $ col03 $;
   datalines;
Invoice &val01.  k
Model   &val02.  un
MSRP    &val03.  mi
;


proc print data=WORK.teste;
run;

The desired result would be:

Obs  col01    col02      col03
1    Invoice  12.846,29  k
2    Model    428        un
3    MSRP     14,03      mi

However, ​​macro variable references are not being interpreted by SAS as ​​macro variable values.

And the result is:

Obs  col01    col02    col03
1    Invoice  &val01.  k
2    Model    &val02.  un
3    MSRP     &val03.  mi

How to make the SAS understand &val01. as a macro variable name, rather than a string.

Anyone would like a tip, please.


Solution

  • You can could use the RESOLVE() function, as long as the lines of data are shorter than the maximum length allowed for a character variable.

    %let val01= 12.846,29;
    %let val02= 428;
    %let val03= 14,03 ;
    
    data test;
      input @;
      _infile_=resolve(_infile_);
      input col01 $ col02 $ col03 $;
    datalines;
    Invoice &val01.  k
    Model   &val02.  un
    MSRP    &val03.  mi
    ;
    

    Or if it is only COL02 that needs to be replaced by the value of a macro variable then have the source data for that column just be the NAME of the macro variable, not an actual macro expression. Then use the SYMGET() function to retrieve the value of the macro variable.

    data test;
      length col01-col03 $10;
      input col01 mvar :$32. col03 ;
      col02 = symget(mvar);
      drop mvar;
    datalines;
    Invoice val01  k
    Model   val02  un
    MSRP    val03  mi
    ;