BookmarkSubscribeRSS Feed
Ramya2
Calcite | Level 5

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. 

6 REPLIES 6
Reeza
Super User

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. 


 

Ramya2
Calcite | Level 5
Hi Reeza, Thank you for the solution. Yeah that Zero was not intentional. I have another quick question. if I want result as &n. and n is macro parameter n=ABC. How do I write it. I tried &&n.. , thinking &n. will resolve macro and remaining &n. will give me a result. But this is quiet Rubbish. Please help me.
Ramya2
Calcite | Level 5
Sorry, Correcting above requirement, need result as &ABC.
Tom
Super User Tom
Super User

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
Calcite | Level 5
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?
Tom
Super User Tom
Super User

@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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 979 views
  • 0 likes
  • 3 in conversation