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

I have the following code with an if-then-else statement.  Both the If and the Else are running.  The value of &nrows being input to this dataset = 1 which meets the IF condition.  I know the Else is running because after the code executes "cancel" = the value from the Else do statement.

 

 

data _NULL_;
     if 0 then set specs_step2 nobs=n;
          if &nrows > 0 then do;
               %put NOTE: Program terminated because there are missing IRNs in the data.;
               %put See the Results or Output window for the cases where these are needed.;
               %put Putstate3 = There are &nrows. IRNs to update.;
               %let cancel = cancel;
          end;
          else do;
               %put NOTE: Continue processing. All IRNs are provided in the dataset.;
               %let cancel = This is the else condition;
          end;
          stop;
run;
%put Cancel = &cancel. ;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Do you just wan this then:

 

        %if &nrows > 0 %then %do;
               %put NOTE: Program terminated because there are missing IRNs in the data.;
               %put See the Results or Output window for the cases where these are needed.;
               %put Putstate3 = There are &nrows. IRNs to update.;
               %let cancel = cancel;
          %end;
          %else %do;
               %put NOTE: Continue processing. All IRNs are provided in the dataset.;
               %let cancel = This is the else condition;
            %end;

@StaceyB wrote:

Okay, it's still not quite working.  

 

In a previous step in the code I'm setting the value of the &nrows.  Now I'd like to set the value of &cancel to either a blank or to the value of cancel and write to the log the corresponding put statements in the code above.  In the following statements I end them with:

run &cancel;  By doing this the remaining code doesn't run based on the value that was in &nrows at the point I'm placing the code in this post.

 

Does that help a little bit?


 

View solution in original post

7 REPLIES 7
Astounding
PROC Star

Macro language statements such as %PUT and %LET are never part of a DATA step.  They cannot be controlled using DATA step IF THEN statements.

 

A DATA step can use PUT statements (not %PUT) to write messages, and can use CALL SYMPUTX to assign values to macro variables.

StaceyB
Obsidian | Level 7

I did have the idea that I was mixing  up the macros with the data step.  I've been changing it around in all different ways. 

 

I will update to put instead of %put and will change the let statement to symputx.  

 

Thanks!

Reeza
Super User
You're mixing macro and data step logic and I have no idea where &nrows comes from. Maybe explain what you're trying to do and we can offer suggestions on how to accomplish that.
StaceyB
Obsidian | Level 7

Okay, it's still not quite working.  

 

In a previous step in the code I'm setting the value of the &nrows.  Now I'd like to set the value of &cancel to either a blank or to the value of cancel and write to the log the corresponding put statements in the code above.  In the following statements I end them with:

run &cancel;  By doing this the remaining code doesn't run based on the value that was in &nrows at the point I'm placing the code in this post.

 

Does that help a little bit?

Reeza
Super User

Do you just wan this then:

 

        %if &nrows > 0 %then %do;
               %put NOTE: Program terminated because there are missing IRNs in the data.;
               %put See the Results or Output window for the cases where these are needed.;
               %put Putstate3 = There are &nrows. IRNs to update.;
               %let cancel = cancel;
          %end;
          %else %do;
               %put NOTE: Continue processing. All IRNs are provided in the dataset.;
               %let cancel = This is the else condition;
            %end;

@StaceyB wrote:

Okay, it's still not quite working.  

 

In a previous step in the code I'm setting the value of the &nrows.  Now I'd like to set the value of &cancel to either a blank or to the value of cancel and write to the log the corresponding put statements in the code above.  In the following statements I end them with:

run &cancel;  By doing this the remaining code doesn't run based on the value that was in &nrows at the point I'm placing the code in this post.

 

Does that help a little bit?


 

StaceyB
Obsidian | Level 7

Perfect.  Thank you.

Tom
Super User Tom
Super User

So you have NROWS macro variable and you want to create CANCEL macro variable.

You can do that with macro code.

%if &nrows > 0 %then %do;
  %let cancel=cancel;
%end;
%else %do;
  %let cancel=;
%end;

If you are not running this inside a macro (or are using an old version of SAS that does not support %IF in open code) you could use a data step instead.

%let cancel=;
data _null_;
  if &nrows > 0 then call symputx('cancel','cancel');
run;

Our use %sysfunc() to call the IFC() function.

%let cancel=%sysfunc(ifc(&nobs > 0,cancel,));

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 630 views
  • 5 likes
  • 4 in conversation