%macro sq (var1=, var2=, var3=, var4=);
proc sql;
select count (distinct USUBJID) INTO:&var1 from ADAE where EYE="A" and &var4;
select count (distinct USUBJID) INTO:&var2 from ADAE where EYE="B" and &var4;
select count (distinct USUBJID) INTO:&var3 from ADAE where &var4;
quit;
%put &var1 &var2 &var3
%mend sq;
%sq( var1=x11, var2=x21, var3=x31, var4=%str(AETERM^=""));
log is clear but program doesn't work .
Add
options mprint mlogic symbolgen;
before the macro is called, examine the log or post it here, but please post code and log using the {i} or running man icon to preserve formatting and increase readability.
You could add
%put _local_;
as first command in an macro to write all parameters with their values to the log.
Dear Andreas
about readability will do next time thanks.
There are log like this.
SYMBOLGEN: Macro variable VAR1 resolves to x11
MLOGIC(SQ): %LET (variable name is x11)
SYMBOLGEN: Macro variable V1 resolves to 11
SYMBOLGEN: Macro variable VAR2 resolves to x21
MLOGIC(SQ): %LET (variable name is x21)
SYMBOLGEN: Macro variable V2 resolves to 5
SYMBOLGEN: Macro variable VAR3 resolves to x31
MLOGIC(SQ): %LET (variable name is x31)
SYMBOLGEN: Macro variable V3 resolves to 14
MLOGIC(SQ): %PUT _local_
SQ V1 11
SQ V2 5
SQ V3 14
SQ VAR1 x11
SQ VAR2 x21
SQ VAR3 x31
SQ VAR4 AETERM""
SQ X11 11
SQ X21 5
SQ X31 14
but when I want to use like
&x11 and get 11 its not working.
Hi Andreas_IDS
Yout code works as intended, except for two problems with the %put statement.
1. a missing semicolon in the %put statement.
2. When you put &var1, you get the content of &var1, which is X31. You must use &&&var1 to resolve further and get the value of
X31.
The noprint option on the proc sql statement is to prevent Proc SQL from generating procedure output in the output window for the 3 selects.
data adae;
USUBJID = 1; EYE = 'A'; AETERM = ''; output;
USUBJID = 1; EYE = 'A'; AETERM = 'X'; output;
USUBJID = 2; EYE = 'B'; AETERM = ''; output;
USUBJID = 2; EYE = 'B'; AETERM = 'X'; output;
USUBJID = 2; EYE = 'B'; AETERM = 'Y'; output;
run;
%macro sq (var1=, var2=, var3=, var4=);
proc sql noprint;
select count (distinct USUBJID) INTO:&var1 from ADAE where EYE="A" and &var4;
select count (distinct USUBJID) INTO:&var2 from ADAE where EYE="B" and &var4;
select count (distinct USUBJID) INTO:&var3 from ADAE where &var4;
quit;
%put &&&var1 &&&var2 &&&var3;
%mend sq;
%sq( var1=x11, var2=x21, var3=x31, var4=%str(AETERM^=""));
Result:
1 1 2
@Vahe_Mar wrote:
log is clear but program doesn't work .
What about it doesn't work? Show us the SASLOG. Show us the results you are getting and describe why they are wrong and what you expect.
@Vahe_Mar wrote:
"why they are wrong?" what you mean?
I am asking for a more detailed explanation. All you said was "doesn't work" and "not working" which tells us nothing.
Show us the entire SAS log. Click on {i} and paste the SAS log into the window that appears.
PLEASE use these buttons for posting logs and code, respectively.
So, let's go back to message 2 of this thread, where @andreas_lds requested:
Add
options mprint mlogic symbolgen;before the macro is called, examine the log or post it here, but please post code and log using the {i} or running man icon to preserve formatting and increase readability.
Please follow the instructions given by @andreas_lds. We're trying to help you, but you have to help us as well.
Sorry Vahe_Mar
I put a wong name in my post. But the code is for you, not Andreas_IDS.
Macro variables created by SQL are local, not global. To make them global, add a line to your macro definition:
%macro sq (var1=, var2=, var3=, var4=);
%global &var1 &var2 &var3;
proc sql;
select count (distinct USUBJID) INTO:&var1 from ADAE where EYE="A" and &var4;
select count (distinct USUBJID) INTO:&var2 from ADAE where EYE="B" and &var4;
select count (distinct USUBJID) INTO:&var3 from ADAE where &var4;
quit;
%put &var1 &var2 &var3
%mend sq;
And if &var4 gives you any trouble (and it might), make the following additional change:
where %unquote(&var4);
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!
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.
Ready to level-up your skills? Choose your own adventure.