BookmarkSubscribeRSS Feed
webart999ARM
Quartz | Level 8

Hello Folks,

 

How can I dynamically check one of the dataset variable values in open code and based on condition is satisfyed or not proceed to the next step or skip that dataset if not satisfied?


This is urgent, and any suggestions will be helpful.

 

Kind Regards
Artur

8 REPLIES 8
SASKiwi
PROC Star

SAS macro language will enable you to do this. However you usually need to have your conditional inside a macro which is by definition not open code.

 

Another option is conditionally generate code in a DATA step using CALL EXECUTE. This avoids macro entirely but is not really designed to run a lot of conditional code.

jimbarbour
Meteorite | Level 14

@SASKiwi.

 

Conditionals no longer have to be inside a macro as of 9.4 M5.  They can only be one level, and they have to have a %DO and a %END, but they don't have to be inside a %MACRO - MEND.

 

Jim

SASKiwi
PROC Star

@jimbarbour - Yes, I was aware of that and thanks for the reminder. There are often many other reasons for putting significant amounts of macro code inside macros and one of those is it is simply good practice 😉

jimbarbour
Meteorite | Level 14

@SASKiwi,

 

Well I suppose there are reasons to put %IF statements inside a macro, but I've been using in-line %IF statements since they were introduced.  I find them very useful.  The only time I've gotten in trouble with them is inside an Include that was conditionally included.  Otherwise they've been really handy.   I find the flow far easier to follow with a simple in-line %IF than a macro.

 

Jim

SASKiwi
PROC Star

@jimbarbour - I'd be fine with that approach too - when we get to at least M5 that is (not there yet)! I was thinking more along the lines of something more complex than a few simple %IFs.

jimbarbour
Meteorite | Level 14

Does it have to be any particular record in the SAS dataset or is it just a variable on the first record?

 

If it's open code, then I'd do a mini DATA step like the below and set a global macro variable.  I'd then have an in-line macro if statement following the mini DATA step that runs the code based on the global macro variable.  If it's really open code, there's no reason to not have a mini DATA step.

 

Jim

 

DATA _NULL_;
    IF  Observations < 1 THEN
        CALL SYMPUTX ('Process_Next_Data', 0, 'G');

    SET About_To_Process_This_Dataset (KEEP=Critical_Variable) NOBS=Observations;

     IF  &Too_Low < Critical_Variable < &Too_High THEN
        CALL SYMPUTX ('Process_Next_Data', 1, 'G');
    ELSE
        CALL SYMPUTX ('Process_Next_Data', 0, 'G');
    STOP;
RUN;

%IF  &Process_Next_Data  %THEN
        %DO;
               ...  Next set of processing code goes here ...
        %END;
ballardw
Super User

@webart999ARM wrote:

Hello Folks,

 

How can I dynamically check one of the dataset variable values in open code and based on condition is satisfyed or not proceed to the next step or skip that dataset if not satisfied?


This is urgent, and any suggestions will be helpful.

 

Kind Regards
Artur


It never ever hurts to provide some small example data and then walk us through how you intend to use the information. Best is to provide data step code to generate the data set then we can test things.

 

It is amazing how often people assume one approach to "conditionals" is needed but other methods are available. A concrete example may reveal such. Otherwise this is a generic question and you get generic responses.

 

The data does not have to be complicated or extensive, just enough records, variables and values to demonstrate what is needed. (Hint: do not make values all 1 and 0 unless your data is actually such, there are some interesting tricks available with such values.) If you are going to have any character variables then make sure they are not all the same value.

Astounding
PROC Star

Artur,

 

Here's a way you can come pretty close to what you ask for.  You can skip steps that end with a RUN; statement.  So that does not include SQL or other procedures that end with a QUIT; statement.  And you will still get a note on your log reflecting steps that were skipped.  If all that is satisfactory, read on.

 

Presumably, you already know how to write a DATA step that can check whether or not you want to skip some subsequent steps.  Within that DATA step, use CALL SYMPUT to create a macro variable with either a blank value or the word CANCEL as the value:

if (subsequent steps should be skipped) then call symput('skip', 'cancel');
else call symput('skip', ' ');

You have to replace the IF condition with the proper conditions ... the logic was never included in your original post.

 

Then you have to take one more step.  All the steps that should be skipped should change their RUN; statement to:

run &skip;

When the step should be skipped, that will come out as:

run cancel;

The word CANCEL causes the step to be skipped, although you still get feedback on the log about the step that was skipped.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 737 views
  • 6 likes
  • 5 in conversation