BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi

im running this script and am getting very weird results, see below.
SCRIPT:
%let mm2 = '01';
data _null_;
if &mm2. in ('01','03','05','07','08','10','12') then do;
%put 'yes';
end;
else do;
%put 'no';
end;
run;

LOG:
910 %let mm2 = '01';
911 data _null_;
913 if &mm2. in ('01','03','05','07','08','10','12') then do;
914 %put 'yes';
'yes'
915 end;
916 else do;
917 %put 'no';
'no'
918 end;
919 run;

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

As you can see it is running both the then and the else?!?!
And ideas?

Thanks

Simon
4 REPLIES 4
LinusH
Tourmaline | Level 20
I think you are mixing macro and data step programming in an unhealthy way. Decide if you want to use macro or data step logic to solve your problem.
In your case, use put statement rather than %put. You can use macro statement to control non macro execution, but you cannot use data step logic to control macro statement execution.

/Linus
Data never sleeps
deleted_user
Not applicable
thanks for the quick response....
so what would be the best way to do the following:

%let mm2 = '01';

if &mm2. in ('01','03','05','07','08','10','12') then do;
***run macro***
end;

Thanks

Spud
LinusH
Tourmaline | Level 20
Not knowing what will happen in the ***run macro*** section, I would probably go for the macro:

%macro xxx(mm2) ;

%if %sysfunc(indexw(01 03 05 07 08 10 12,&MM2)) %then %do;
%put Macro is running....;
%end;
%else %put No macro runs.;

%mend xxx;

%xxx(01);

If you are on 9.2, you should be able to use the IN operator with %if, which would make the code a bit more neat.

/Linus
Data never sleeps
Cynthia_sas
SAS Super FREQ
Hi:
I agree with the comment that you are mixing data step concepts and macro concepts in a possibly inappropriate way.

The %PUT statement is intended to help you debug macro processing by writing macro variable values and/or text strings to the SAS log. It is really not intended to be used in the way you attempt in your program (conditionally). Consider these 2 LOG outputs from one program that uses %PUT and another that uses PUTLOG: (There is NO student named 'Zorro' in the sashelp.class file.):
[pre]
18961 *** 1) try %PUT;
18962 data _null_;
18963 set sashelp.class;
18964 if name = 'Alfred' then do;
18965 %put 'Alfred';
'Alfred'
18966 end;
18967 else if name = 'Zorro' then do;
18968 %put 'Zorro';
'Zorro'
18969 end;
18970 run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


18971
18972 *** 2) Use PUTLOG;
18973 data _null_;
18974 set sashelp.class;
18975 if name = 'Alfred' then do;
18976 putlog 'Alfred';
18977 end;
18978 else if name = 'Zorro' then do;
18979 putlog 'Zorro';
18980 end;
18981 run;

Alfred
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
[/pre]

Also, I generally find it unecessary to use quotes when setting macro variables (as in a %LET statement) because the inclusion of quotes for the macro variable value MIGHT work in your single instance, but then if you wanted to use &mm2 in a title statement, for example, then if you did this (and inadvertantly used double quotes):
%let mm2 = "01";
title "this is the report for the month &mm2";

you would get this in the log:
[pre]
18991 %let mm2 = "01";
SYMBOLGEN: Macro variable MM2 resolves to "01"
18992 title "this is the report for the month &mm2";
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.
NOTE: Line generated by the macro variable "MM2".
1 "this is the report for the month "01"
-----------------------------------
49

NOTE 49-169: The meaning of an identifier after a quoted string may change in a future SAS release.
Inserting white space between a quoted string and the succeeding identifier is
recommended.

[/pre]

This code might work for you, depending on what the rest of your macro program is doing:
[pre]
%let mm2 = 01 ;
... ...
if "&mm2." in ('01','03','05','07','08','10','12') then do;

[/pre]

Here are some good papers that discuss macro basics and issues with quotes and how to use macro "quoting" functions:
http://www2.sas.com/proceedings/sugi29/243-29.pdf
http://www2.sas.com/proceedings/forum2007/152-2007.pdf
http://www2.sas.com/proceedings/sugi27/p067-27.pdf
http://ssc.utexas.edu/docs/sashelp/sugi/23/Coders/p81.pdf

cynthia

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

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
  • 4 replies
  • 667 views
  • 0 likes
  • 3 in conversation