Hi Team,
I have a scenario like, After resolving a macro I need the result as &n. How do I get this.
example:
%macro print(into=);
proc sql;
select strip(put(count(distinct USUBJID), 8.)) into: &into0. separated by ' '
from XYZ.;
Quit;
%mend;
%print(into=ABC);
%put &ABC.; ---> this step is not giving me any result. I can keep this inside a macro, But when I have more than one macro calls then this will not work. Kindly anyone please help.
Macro variable scope, the macro variable exists within the macro but does not exist outside. If you want to access it outside the macro, create it as a global macro variable.
%global &into;
Adding that into your macro should solve your issues.
That 0 may cause issues though, not sure if that's intentional or not.
select strip(put(count(distinct USUBJID), 8.)) into: &into0. separated by ' ' from XYZ.;
That would create a macro variable called ABC0, not ABC.
@Ramya2 wrote:
Hi Team,
I have a scenario like, After resolving a macro I need the result as &n. How do I get this.
example:
%macro print(into=);
proc sql;
select strip(put(count(distinct USUBJID), 8.)) into: &into0. separated by ' '
from XYZ.;
Quit;
%mend;
%print(into=ABC);
%put &ABC.; ---> this step is not giving me any result. I can keep this inside a macro, But when I have more than one macro calls then this will not work. Kindly anyone please help.
Assuming the extra 0 is just a type there are two issues here.
First a scoping issue. You are calling a macro and asking it to create a macro variable named ABC. If no sure macro variable exists when the macro runs then it will be created in the macro's scope. Which means it will then disappear after the macro runs.
Either define the macro variable first.
%let abc=;
%print(into=abc);
Or make the macro smarter.
%macro print(into=);
%if not %symexist(&into) %then %global &into;
...
Second is that the INTO clause will not create (or overwrite) the target macro variable(s) if no observations are returned by the query. To fix that you should set the macro variable to some default values before running the query.
%macro print(into=);
%if not %symexist(&into) %then %global &into;
%let &into=;
proc sql noprint;
select count(distinct USUBJID) format=32.
into :&into. separated by ' '
from XYZ
;
quit;
%mend print;
@Ramya2 wrote:
Wow. It is a detailed solution. Understood where I went wrong. Thank you. Another question, if I need a result: &ABC. ,after resolving a macro variable print(n=ABC). How will write in program?
After calling the macro with %PRINT(into=ABC) then the macro variable ABC with have the number of distinct patients in the dataset ZYX. You can then expand the value of that macro variable by using &ABC anywhere you would normally type an integer.
data test;
n_subjects = &abc ;
run;
%put Number of subjects in XYZ is &abc.. ;
Only exception is that macro triggers, & and %, are not evaluated inside of strings that single quotes on the outside instead of double quotes. So check out the difference results produced by these statements.
data _null_;
put "Number of subjects is &abc..";
put 'Number of subjects is &abc..';
put "Number of subjects is '&abc'.";
put 'Number of subjects is "&abc".';
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.