BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

Is there a way for me to mimic the use of %if outside of macro in SAS9.4M1?

I know SAS9.4M5 enables to use %if outside of macro, which is really good. Unfortunately, I use SAS9.4M1, but I would like to use this functionality, so I'm looking for a way to bypass it. 

9 REPLIES 9
ChrisNZ
Tourmaline | Level 20

Yes. Easily:

 

data WANT;

  set %sysfunc(ifc(&var=1, WORK.TEST1, WORK.TEST2 ));

run;

braam
Quartz | Level 8

@ChrisNZ 

I'm sorry that I didn't get your point clearly.

I have several lines of codes that I would like to be run only if a certain condition is satisfied. The code may include data steps and some procedures.

 

 

PaigeMiller
Diamond | Level 26

Several lines of code that are run depending on if a condition is satisfied in SAS 9.4M1?


You have the perfect tool that works for millions of SAS users when they had SAS 9.4M1. Write a macro with a %IF statement.

 

Why don't you want to do it that way?

--
Paige Miller
braam
Quartz | Level 8
Well, I've used %if outside of macro in SAS9.4M5 before (with a different computer), and I liked its functionality in that it is more flexible and easy to debug and change. Unfortunately, I don't have access to that version of SAS, so I want to achieve something similar if possible.
PaigeMiller
Diamond | Level 26

Achieve something similar by creating a macro with the desired %IF 

--
Paige Miller
Tom
Super User Tom
Super User

So you had something like:

%if &x=5 %then %do;
  data  ....
  proc ...
%end;

Convert it to:

%macro mymacro;
%if &x=5 %then %do;
  data  ....
  proc ...
%end;
%mend mymacro;
%mymacro;

You could try to get tricky with stuffing the code into a macro variable, or into a file, but it will be much easier to use a macro.  Just be careful if the block of code is used to create macro variables.  Unless you tell it otherwise any new macro variable created inside a macro is local to the macro and disappears when the macro finishes running.

Kurt_Bremser
Super User

Upgrade your SAS version, or have it upgraded. The current SAS version is always available for your license.

Or wrap into a macro, as we always did before 9.4M5.

Another application of Maxim 45.

ChrisNZ
Tourmaline | Level 20

You can do:

%sysfunc(ifc(&var=1,

  data WANT;

    set WORK.TEST1;  

  run;

,

  data WANT;

    set WORK.TEST2;  

  run;

));

but then you'll probably run into quoting issues, and you're better off using a macro or call execute() as mentioned.

LeonidBatkhan
Lapis Lazuli | Level 10

Hi braam,

You can do what you want by using CALL EXECUTE.

Let's say you want to replicate the following:

%let P=PRINT;
%if &P=PRINT %then %do;
   proc print data=a;
   run;
%end;

You can do this with a data step as follows:

%let P=PRINT;
data _null_;
   if "&P"="PRINT" then call execute('proc print data=a; run;');
run;

Please take a look at my blog post CALL EXECUTE made easy for SAS data-driven programming for detailed explanation.

Hope this helps.

 

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
  • 9 replies
  • 835 views
  • 1 like
  • 6 in conversation