BookmarkSubscribeRSS Feed
jjjames
Calcite | Level 5

Thanks again for all the replies.

As Tom suggested^^ the external files are both .sas files with simple hard coded assignment statements. E.g.

File A:

width=196;

length=100;

depth=250;

File B:

width=200;

length=450;

depth=65;


there is a process elsewhere which reads an excel data set and generates this simple code ^ which I need to read in conditionally on my variable 'colour' being either "red" or "blue"


thanks again

Tom
Super User Tom
Super User

So in that case you do not want to conditionally include the files, instead you want to conditionally execute the code that the files contain.  So place the %INCLUDE statements inside of logic blocks so that they execute at the appropriate place.  Because you are not reading these variables from either a source dataset or via INPUT statement the values will by default be retained, so you might want to worry about clearing the values.

data want ;

  set have;

  if colour = 'BLUE' then do;

%include 'file A'

end;

else if colour='RED' then do;

%include 'file B';

end;

output;

call missing (of _all_);

run;

Peter_L
Quartz | Level 8

I use the following method (careful with the semicolons):

1. Define a macro like this:

%macro do_if(a);

%if &a %then

  %let c=;

%else

  %let c=*;

&c

%mend do_if;

2. Use it like this:

%do_if(&var = red) %include "red.sas";

It works by commenting out the rest of the line up to the final semicolon when the condition is false. An even simpler way is to use a variable which could contain * and just place it at the start of the line. The logic is not so self-documenting.

&star %include "blue.sas";

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 17 replies
  • 12343 views
  • 3 likes
  • 9 in conversation