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. ;
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?
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.
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!
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?
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?
Perfect. Thank you.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.