Hi All,
I am trying to create macro variables using proc sql within a macro.But the code does not work. its pretty simple code but I do not understand why its not working.
data have;
input var;
datalines;
12
34
8
79
84
;
run;
%macro m1;
proc sql noprint;
select count(var) into :cnt from have ;
quit;
Proc sql noprint;
select var into :val1 - :val%left(&cnt) from have;
Quit;
%mend;
%m1;
%put &val1 &val2 &val3 &val4 &val5;
There are two ways to make the macro variables global:
1) Remove macro definition completely.
2) Using %global statement
Otherwise they are local, they still can be printed out within your macro though.
Haikuo
There are two ways to make the macro variables global:
1) Remove macro definition completely.
2) Using %global statement
Otherwise they are local, they still can be printed out within your macro though.
Haikuo
what is the error your getting? I think you cannot use %left function . What you can do %let cnt=%trim(&cnt) before the second query.
There is no problem with the %left. This is as Haikuo already stated a problem with local vs global macro variables.
Removing the macro definition seems to the most reasonable action, having the code wrapped up inside a macro does not add anything...
Yes ,You Have to mention %global in your macro as said by .
or else you have to create all the macro variables with dummy values before the macro.
like
data have;
input var;
datalines;
12
34
8
79
84
;
run;
%let Val1=0;
%let Val2=0;
%let Val3=0;
%let Val4=0;
%let Val5=0;
%macro m1;
proc sql noprint;
select count(var) into :cnt from have ;
quit;
Proc sql noprint;
select var into :val1 - :val%left(&cnt) from have;
Quit;
%mend;
%m1;
%put &val1 &val2 &val3 &val4 &val5;
But i always prefer to use %Global.
Regards.
Sanjeev.K
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.