Hi Forum,
I have this dataset.
data CCCC;
input yearmonth age;
cards;
201112 10
201012 20
201207 30
;
run;
I need to pick the latest vlaue of variable named yearmonth (which is 201207) and feed that value into sql code below.
/*SQL code*/
proc sql;
select age
from anyname
where yearmonth = '&latest';
run;
My approach
proc sort data=CCCC out= anyname;
by descending yearmonth;
run;
data _null_;
set anyname;
if _n_=1 then do;
call symput('latest',yearmonth);
stop;
end;
run;
problem
An error messege comes.
ERROR: Expression using equals (=) has components that are of different data types.
Could anyone please help me to fix this.
Thanks
Mirisage
Yearmonth is a numeric variable, but macrovar can only be characters. You can
call symput('latest',cats(yearmonth));
which takes advantage of the behavior of the CAT functions to automatically convert number values to character.
Before the advent of the CAT functions, we would
call symput('lastest',put(yearmonth,8.0-L));
Yearmonth is a numeric variable, but macrovar can only be characters. You can
call symput('latest',cats(yearmonth));
which takes advantage of the behavior of the CAT functions to automatically convert number values to character.
Before the advent of the CAT functions, we would
call symput('lastest',put(yearmonth,8.0-L));
Mark already answered your question about creating a macro variable, but I have to ask: why not just get the result from a single proc sql run? e.g.:
proc sql;
select age
from CCCC
having yearmonth = max(yearmonth);
;
quit;
The WHERE clause will need to use double quotes, not single quotes:
where yearmonth = "&latest";
Macro variable references in single quotes do not get resolved.
Regarding CALL SYMPUT, the earlier comment is correct. An easier solution would be to add a single letter:
call symputX('latest', yearmonth);
Good luck.
Hi Mkeintz, Art and Astounding,
Thank very much every one of you for this excellent clarification.
Hi Art,
What you have suggested is also quite correct.
Warm regards
Mirisage
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.