I am getting macro variables and need write-out their values into file line-by-line.
Somehow, confused into passing the value into dataset.
Totally alike code is below.
data _temp;
set sashelp.class;
ind=_N_;
run;quit;
proc sql noprint;
select age into: val_age from _temp having ind=max(ind);
select height into: val_height from _temp having ind=max(ind);
select weight into: val_weight from _temp having ind=max(ind);
quit;
%put "val_age=&val_age.";
data _out_c;
input line $64.;
datalines;
[value]
age="&val_age."
height=&val_height.
weight=&val_weight.
run;quit;
Tried various combinations, it does not show up values in dataset _out_c. Anyone?!
Why do you want to do this? Seems to be part of a bigger problem? In that case, describe your actual problem. There is probably a better way than this 🙂
If your question is merely related to using macro variables in cards/datalines, read Example 12: Using Macro Variables within a CARDS or DATALINES Statement.
Why do you want to do this? Seems to be part of a bigger problem? In that case, describe your actual problem. There is probably a better way than this 🙂
If your question is merely related to using macro variables in cards/datalines, read Example 12: Using Macro Variables within a CARDS or DATALINES Statement.
@hellohere wrote:
I am getting macro variables and need write-out their values into file line-by-line.
Somehow, confused into passing the value into dataset.
Totally alike code is below.
data _temp; set sashelp.class; ind=_N_; run;quit; proc sql noprint; select age into: val_age from _temp having ind=max(ind); select height into: val_height from _temp having ind=max(ind); select weight into: val_weight from _temp having ind=max(ind); quit; %put "val_age=&val_age."; data _out_c; input line $64.; datalines; [value] age="&val_age." height=&val_height. weight=&val_weight. run;quit;
Tried various combinations, it does not show up values in dataset _out_c. Anyone?!
You have to put assignment statements into the CODE of the data step. Not into any in-line source data text.
data _out_c;
age="&val_age.";
height=&val_height.;
weight=&val_weight.;
run;
If you want to WRITE values then just use the macro variables to generate the PUT statement to WRITE the lines.
data _null_;
file 'name_of_file_you_want_to_write' ;
put "age=""&val_age."""
/ "height=&val_height."
/ "weight=&val_weight."
;
run;
Also why are you putting quotes around the AGE value? Is AGE a character variable?
Also your SQL is inefficient.
proc sql noprint;
select age ,height ,weight
into :val_age
, :val_height
, :val_weight
from _temp
having ind=max(ind)
;
quit;
First of all, DATALINES and macro don't go together. Macro triggers in DATALINES are not resolved, and you cannot use DATALINES in a macro.
DATALINES is always the last statement in a data step and constitutes a step boundary. Therefore, no RUN is needed, and any data step code after it causes a syntax error.
It looks as if you want to use values from the last observation of one dataset in code creating another dataset.
To retrieve a value from the last observation quickly, do this:
data _null_;
set sashelp.class nobs=nobs point=nobs;
call symputx("val_age",age);
stop;
run;
How you need to use the macro variable depends on the code where it should be used, so please show us that.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.