BookmarkSubscribeRSS Feed
jjjames
Calcite | Level 5

I have two external files - both containing SAS code.

Both files contain the same variables so I do not want to include both files in all cases

I would like to use a %INCLUDE to pull in either of these based on the value of a variable in open code.

i.e.

If colour = blue then %INCLUDE "Filepath/FileA"

If colour = red then %INCLUDE "Filepath/FileB"

Is there a relatively straightforward way to accomplish this?

Thanks

17 REPLIES 17
DBailey
Lapis Lazuli | Level 10

%let colour=blue;

data _null_;

if "&colour"="blue" then do;

     call symput('fname','FilePath/FileA');

     end;

else do;

     call symput('fname','FilePath/FileB');

     end;

run;

%include "&fname";

SteveNZ
Obsidian | Level 7

From the manual:

Because %INCLUDE is a global statement and global statements are not executable, the %INCLUDE statement cannot be used in conditional logic

PaigeMiller
Diamond | Level 26

Seems like you would need a macro, and then conditionally issue one %include statement or the other one based on the colour.

Alternatively, if you don't want to do macros, you could use the LINK statement to branch to the code in FileA or FileB, but that code would have to be included in the datastep somehow.

if colour='blue' then Link FileA;

else if colour='red' then Link FileB;

return;

FileA:

/* File A code goes here */

return;

FileB:

/* File B code goes here */

return;

Don't forget your semi-colons

--
Paige Miller
jjjames
Calcite | Level 5

Thanks for your reply, so it would look something like this:

if colour='blue' then Link FileA;

else if colour='red' then Link FileB;

return;

FileA:

%include "filepath/fileA";

return;

FileB:

%include "filepath/fileB";

return;

I originally tried to do something with macros, but both %include statements seemed to run regardless of the colour:

%macro macroA;

%include "filepath/fileA";

%mend;


%macro macroB;

%include "filepath/fileB";

%mend;


if colour='blue' then call execute ("macroA");

else if colour='red' then call execute ("macroB");

PaigeMiller
Diamond | Level 26

FileA:

%include "filepath/fileA";

return;

I wasn't envisioning a %include here, and I haven't specifically ever done it this way. I don't think it would work that way.

What I was envisioning was copying and pasting the code from FileA to replace the %include statement. Same for FileB.

And of course, whatever is in FileA and FileB must be code that can execute in a single data step.

DBailey has explained how to do this with macros.

--
Paige Miller
KarlK
Fluorite | Level 6

For an advanced discussion of this topic, I would recommend:

http://www.sascommunity.org/wiki/Conditionally_Executing_Global_Statements

The author, Ron Fehd, has forgotten more about SAS coding than I can begin to know, and I find myself going back to this article every so often to solve a complex conditional execution problem with some elegant IFC() coding magic.

HTH,

Karl

jjjames
Calcite | Level 5

I read that page and see that this code can be used to conditionally execute % includes by replacing the %put with the %include.

However, how do i set options conditional on colour?

options source2;

%sysfunc(ifc(%sysfunc(getoption(source2)) eq SOURCE2

  ,%nrstr(%put true*;)

  ,%nrstr(%put *false;)

  ));

options nosource2;

%sysfunc(ifc(%sysfunc(getoption(source2)) eq SOURCE2

  ,%nrstr(%put true*;)

  ,%nrstr(%put *false;)

  ));

SASKiwi
PROC Star

You can easily do conditional %INCLUDEs using CALL EXECUTE:

%let colour = blue;

data _null_;

  colour = "&colour";

  if colour='blue' then call execute ('%include "program1.sas";');

  else if colour='red' then call execute ('%include "program2.sas";');

run;

You just need to make sure that whatever you put in your call execute is a syntactically-correct statement(s).

jjjames
Calcite | Level 5

I originally tried to use call execute...but that code seems as though it would always execute program 1?

%let colour = blue;

data _null_;

  colour = "&colour";

  if colour='blue' then call execute ('%include "program1.sas";');

  else if colour='red' then call execute ('%include "program2.sas";');

run;

Tom
Super User Tom
Super User

You need to explain more what you want to happen and when.  You mention two code files and in the same breath talk about variables, as if you mean data files instead of code files.  Then you use IF statements that make it seem as if you want different things to happen for different observations of the same data step.

So what do the two files do?  Are they full programs with multiple data and proc steps?  Are they fragments of code to be embedded into a single data step?

What is the IF COLOR='blue' condition being test against? Is it a single observation of a data set?  If so is it possible that a single dataset will have records with both blue and red values?

If you want to conditionally determine which piece of code is include perhaps you can conditionally generate a FILEREF and then use the fileref in the %INCLUDE statement.

data _null_;

   if "&color" = "blue" then filename('STEP2',"file1.sas");

  else if "&color" = "red" then filename('STEP2',"file2.sas");

run;

%include step2;

jjjames
Calcite | Level 5

Thanks for your reply and sorry about being unclear I am relatively new to coding.

So in a data step I have a variable 'colour' which can take either of two values: "red" or "blue" and I have two external files which contain several numerical variables hard coded to certain values. Each external file contains different values for each variable. These external files are updated elsewhere so I cannot change this format (Using % include). I basically want to include the one set if colour is blue and the other if red.

Thanks again

Cynthia_sas
SAS Super FREQ

Hi:

  I'm having a hard time envisioning WHAT your "red" or "blue" files actually contain. At first, it sounded like they contained program code, because you said that they contained code. But now, in your more recent explanation, you said that the external files "contain several numerical variables hard-coded to certain values" -- that doesn't sound like code, that sounds like data. And, you further explained that the external files "are updated elsewhere so you cannot change this format" -- again ,that sounds like DATA, not CODE.

  So, now, the problem sounds different than it did initially. Can you post an example of what is in the first file with the "red" or "blue" and example of the code or data in the 2 external files? I really feel like a concrete sample of your data would help and would eliminate the guessing about what you have and what you want.

  When you say that you want to "include the one set if colour is blue" and "the other if red" almost sounds to me like you want to go read a different external file based on some other variable value in the first file. Again, unless the two external files have "datalines" embedded in the program code, I am not sure now that the %include method is the right method.

  The rule of thumb, when trying to code a macro solution to anything is to start with a working SAS program without any macro references.  So, do you have PROGRAM1.SAS and PROGRAM2.SAS to "include" or do you have DATA1.TXT and DATA2.TXT that you want to read conditionally? Can you show examples of the "real" code that you have and what is in your 3 files? That would be a big help.

cynthia

Tom
Super User Tom
Super User

Post examples please.

Guessing from your description you have two program files with hard coded SAS assignment statements.

Such as:

weight=105;

length=47.5 ;

If so then you want to %INCLUDE both files, but place the %INCLDUE's inside of IF /THEN/ELSE contruct.

data new ;

  set old;

  if color='blue' then do;

%include 'bluefile.sas';

end;

else do;

%include 'redfile.sas';

end;

run;

If instead the files contain just data (such as a CSV file or other ways to store data) the solution would be different.

PaigeMiller
Diamond | Level 26
Thanks for your reply and sorry about being unclear I am relatively new to coding.

So in a data step I have a variable 'colour' which can take either of two values: "red" or "blue" and I have two external files which contain several numerical variables hard coded to certain values. Each external file contains different values for each variable. These external files are updated elsewhere so I cannot change this format (Using % include). I basically want to include the one set if colour is blue and the other if red.

It would still help to see an example of these external files.

Nevertheless, it sounds like instead of %include, you could simply use a SAS data step to read the contents of the external file. Then your branching is much simpler, using a macro and macro variables.

%macro oink;

data _null_;

     call symputx('colour',colour);

run;

%if &colour=blue %then filename "file A"%str(;);

%else %if &colour="red" %then filename "file B"%str(;);

%mend;

%oink

...

followed by the appropriate INPUT statements in a data step.

Message was edited by: Paige Miller Code should have been macro statements, this is corrected now.

--
Paige Miller

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 17 replies
  • 9350 views
  • 3 likes
  • 9 in conversation