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 ;

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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 ;

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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.)

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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()))) ) ;

 

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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