BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ChrisWoo
Obsidian | Level 7

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");

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

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 Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

7 REPLIES 7
Quentin
Super User

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 Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
ChrisWoo
Obsidian | Level 7

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 ;
Quentin
Super User

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.)

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Quentin
Super User

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()))) ) ;

 

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
ChrisWoo
Obsidian | Level 7
Thx, that is helpful
ballardw
Super User

Also, the macro variable g_nobs is LOCAL to the macro testing. So it no longer exists after the %testing macro stops executing.

Tom
Super User Tom
Super User

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

 

SAS Innovate 2025: Register Now

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 Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1173 views
  • 3 likes
  • 4 in conversation