DATA Step, Macro, Functions and more

Merge as part of an IF statement

Reply
N/A
Posts: 1

Merge as part of an IF statement

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

Super User
Super User
Posts: 7,039

Re: Merge as part of an IF statement

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;

Super User
Posts: 11,343

Re: Merge as part of an IF statement

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.


Super User
Posts: 5,497

Re: Merge as part of an IF statement

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.

Ask a Question
Discussion stats
  • 3 replies
  • 206 views
  • 2 likes
  • 4 in conversation