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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.