Hello!
How do I access a specific row and column from a dataset and save it to a global variable for later use? for example from sashelp.cars dataset how do I get first row value for 'Make' column?
%macro ExtractACell(rownum=1, VarName='Make'); data work.TempDelete; set sashelp.cars; if _n_ = &rownum then do; %global MyVar; MyVar = ????? *Code to get the Make value from the first row and save to MyVar end; run; %mend ExtractACell; %put &MyVar *<-- Is this correct
Hi,
you can do it for example like that:
%macro ExtractACell(rownum=1, VarName=Make);
data _null_;
set sashelp.cars(obs=&rownum. firstobs=&rownum. keep = &VarName.);
call symputx('MyVar', &VarName., "G");
stop;
run;
%mend ExtractACell;
%ExtractACell(rownum=7, VarName=Model)
%put &MyVar.;
Bart
Hi,
you can do it for example like that:
%macro ExtractACell(rownum=1, VarName=Make);
data _null_;
set sashelp.cars(obs=&rownum. firstobs=&rownum. keep = &VarName.);
call symputx('MyVar', &VarName., "G");
stop;
run;
%mend ExtractACell;
%ExtractACell(rownum=7, VarName=Model)
%put &MyVar.;
Bart
Thank you.
You’re missing the CALL SYMPUTX() to create the macro variable. YOu may want to also use the FIRSTOBS and OBS data set options to read only a single line.
data _null_;
set sashelp.cars(obs=&rownum firstobs=&rownum);
call symputx(‘myVar’, &varName, ‘g’);
run;
%put &myVar;
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.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.