I'm wondering if I misunderstood the %put macro statement.
Why I cant get "Option C" in my output table?
%macro testing;
%let g_nobs=100000;
%if &g_nobs = 0 %then %do;
%put Option A;
%end;
%if 1 < &g_nobs and &g_nobs <= 10000 %then %do;
%put Option B;
%end;
%if &g_nobs > 10000 %then %do;
%put Option C;
%end;
%mend testing;
proc sql;
create table haha (text char);
insert into haha values ("&g_nobs");
The %PUT statement is simply for writing values to the SAS log. If you run the macro by submitting %testing, it will write Option C to the SAS log.
It sound like you might be wanting to make a function-style macro that will return a bit of text, in this case it would return the text Option C, is that right?
This macro will return the text Option C:
%macro foo;
Option C
%mend foo;
You could use like:
%macro foo;
Option C
%mend foo;
proc sql;
create table haha (text char);
insert into haha values ("%foo")
;
quit ;
If you the macro to decide which text to return based on some value, typically you would add a parameter to the macro rather than use a global macro variable. This could be done like:
%macro testing(nobs=);
%if &nobs = 0 %then %do;
Option A /*return*/
%end;
%else %if 1 < &nobs and &nobs <= 10000 %then %do;
Option B /*return*/
%end;
%else %if &nobs > 10000 %then %do;
Option C /*return*/
%end;
%mend testing;
proc sql;
create table haha (text char);
insert into haha values ("%testing(nobs=0)") ;
insert into haha values ("%testing(nobs=5000)") ;
insert into haha values ("%testing(nobs=15000)") ;
quit ;
The %PUT statement is simply for writing values to the SAS log. If you run the macro by submitting %testing, it will write Option C to the SAS log.
It sound like you might be wanting to make a function-style macro that will return a bit of text, in this case it would return the text Option C, is that right?
This macro will return the text Option C:
%macro foo;
Option C
%mend foo;
You could use like:
%macro foo;
Option C
%mend foo;
proc sql;
create table haha (text char);
insert into haha values ("%foo")
;
quit ;
If you the macro to decide which text to return based on some value, typically you would add a parameter to the macro rather than use a global macro variable. This could be done like:
%macro testing(nobs=);
%if &nobs = 0 %then %do;
Option A /*return*/
%end;
%else %if 1 < &nobs and &nobs <= 10000 %then %do;
Option B /*return*/
%end;
%else %if &nobs > 10000 %then %do;
Option C /*return*/
%end;
%mend testing;
proc sql;
create table haha (text char);
insert into haha values ("%testing(nobs=0)") ;
insert into haha values ("%testing(nobs=5000)") ;
insert into haha values ("%testing(nobs=15000)") ;
quit ;
I would like to seek your help again on my %if condition.
I tried to modified abit from previous code, but why I cant get my result "F" even if today_day = 12.
%let PrevMonth = %sysfunc(intnx(month,%sysfunc(today()),-1,s),mmddyy2.);
%let Today_day = %sysfunc(intnx(day,%sysfunc(today()),0,s),ddmmyy2.);
%macro testing(date=&Today_day);
%if 1 <= &date < 3 %then %do;
T /*return*/
%end;
%else %if 3 <= &date <= 31 %then %do;
F /*return*/
%end;
%mend testing;
proc sql;
create table haha (text char);
insert into haha values ("%testing(date=&today_day)");
quit ;
The macro language evaluates:
1 <= &date < 3
As being
(1 <= &date) < 3
In the macro language, you can't do that chained comparison thing. Try:
(1 <= &date) and (&date < 3)
(Parentheses are optional here, but help clarify the intent.)
Side note, if just want to see what the macro returns, that is when you can use a %PUT statement, outside of the macro. So instead of using SQL to see a value, you can run the macro and look at your log. Code like:
%macro testing(date= );
%if (1 <= &date) and (&date < 3) %then %do;
T /*return*/
%end;
%else %if (3 <= &date) and (&date <= 31) %then %do;
F /*return*/
%end;
%mend testing;
%put date=2 returns: %testing(date=2) ;
%put date=8 returns: %testing(date=8) ;
%put date=today() returns: %testing(date=%sysfunc(day(%sysfunc(today()))) ) ;
Also, the macro variable g_nobs is LOCAL to the macro testing. So it no longer exists after the %testing macro stops executing.
@ballardw wrote:
Also, the macro variable g_nobs is LOCAL to the macro testing. So it no longer exists after the %testing macro stops executing.
That macro did NOT define G_NOBS as LOCAL. It will only create a local macro variable if there is not already a macro variable with that name.
So if you want to see the value after the macro has finished running just make sure you have created a macro variable named G_NOBS before you call the macro.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.