BookmarkSubscribeRSS Feed
kklein
Calcite | Level 5

I'm new to macro coding.  I'm trying to check a previously-set global variable to see it I want to merge two datasets or just SET  to a single one.  Here's the code from SYSLOG:

228        DATA NT180 NTONL180 NTZZCHK BOTHBAD WINONLY RACFONLY ;   

SYMBOLGEN:  Macro variable NTON resolves to no                      

229           if &nton = no then do ;                               

230              set work.merged ;                                  

231           end ;                                                 

SYMBOLGEN:  Macro variable NTON resolves to no                      

232           if &nton = yes then do ;                              

233              MERGE MERGED(IN=RACFIN) NTSIGNON(IN=NTIN) ;        

ERROR: File WORK.NTSIGNON.DATA does not exist.                      

234                BY RACFID ;                                      

235           end ;                                                

My problem is with the MERGE stmt being looked at.  I would think it wouldn't be since the &nton = yes will not be true.

I'm guessing there's something basic to explain this, but I haven't been able to find it (currently rereading the SAS Macro manual).

P.S.  SAS 9.3 on z/OS 1.13

3 REPLIES 3
Tom
Super User Tom
Super User

You have a couple of issues going on here.

You are trying to test using datastep IF statement the value of a macro variable.  Since all macro variables are text strings you probably would want to use quotes around the values so that you are generating a valid SAS statement.  Your current IF statement is actually testing if the variable NO is equal to the variable NO.

Second you want to use macro logic to conditionally generate the SAS code to avoid the error message about missing dataset.  This will require that you wrap the code inside of a macro definition and then invoke the macro.

%macro testit ;

DATA NT180 NTONL180 NTZZCHK BOTHBAD WINONLY RACFONLY ;

%if (%upcase(&nton) = NO) %then %do;

       set work.merged ;                                 

%end;                                               

%else %do;

       MERGE MERGED(IN=RACFIN) NTSIGNON(IN=NTIN) ;

       BY RACFID ;                                     

%end;

RUN;

%mend testit;

%let nton=NO;

%testit;

ballardw
Super User

I think you want to use the MACRO if

%if &nton=no %then %do;

BUT the %if statement is not allowed in open code. You will need to use an actual macro

%macro mymacroname();

<code>

%mend;

to define and then

%mymacroname();

to execute.

Your code is creating a data step that looks like:

if no=no then do;

     set work.merged;

end;

if no=yes then do;

<merge>

end;

Since the variables no and yes are probably undefined they are both missing and missing=missing so it attempts both parts of the code.

Look at your data set and you should find the variables no and yes with missing values.


Astounding
PROC Star

As others have pointed out, your best solution would be to actually define a macro, so that you can use %IF %THEN statements.  As Tom hinted, in this case you could get by with a simpler solution as well by adding double quotes to your comparisons:

if "&nton" = "yes" then do;

...

if "&nton" = "no" then do;

...

It will run a bit slower (and will make others question your macro language skills), but it will get you past the immediate problem.

Good luck.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

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