%macro getValueOf(var, data=_last_, obsno=1);
%global getValueOf_ds;
%local rc;
%if &getValueOf_ds= %then %do;
%let getValueOf_ds=%sysfunc(open(&data,i));
%if &getValueOf_ds=0 %then %goto cleanUp;
%end;
%syscall set(getValueOf_ds);
%let rc = %sysfunc(fetchobs(&getValueOf_ds, &obsno));
%if &rc ^= 0 %then %goto cleanUp;
%*;&&&var
%return;
%cleanUp:
%if &getValueOf_ds>0 %then %let rc = %sysfunc(close(&getValueOf_ds));
%symdel getValueOf_ds;
%mend getValueOf;
/* checking */
data class;
set sashelp.class(obs=1);
run;
%put name=%getValueOf(name);
%put age =%getValueOf(age);
%*-- on log
name=Alfred
age =14
--*;
%getValueOf(obsno=0) %*-- to close the data set --*;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.