BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
amit7urmc
Fluorite | Level 6

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

4 REPLIES 4
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Reeza
Super User

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 19637 views
  • 1 like
  • 3 in conversation