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.
Macro facility does not accept CARDS or PARMCARDS statements. See https://support.sas.com/kb/43/902.html
And if you really want to do this, here are examples using different ways( quote()
function or proc stream
): https://support.sas.com/kb/43/295.html
How to use macro variable in datalines?
On the other hand, using cards
statement is not valid in macro definition. See
https://technologytales.com/2008/10/28/sas-macro-and-datalinecards-statements-in-data-step/
The anwser about this question is from the first link, I copied it and write here:
Most of the SAS Language and all of the SAS Macro Language are 'free format' giving the ability to format and indent code for better readability. When compiling a macro, the SAS Macro Facility strips out any formatting (spaces, line breaks, tabs, etc.) in the model text to minimize the size of the stored text.
DATALINES, CARDS, and PARMCARDS statements are not free format. When the SAS Macro Facility compiles these statements, it removes the formatting, which would very possibly move data into incorrect lines and/or columns. This can cause incorrect results and is the fundamental reason for the restriction.
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
;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.