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";

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