BookmarkSubscribeRSS Feed
rrempala
Calcite | Level 5

I am recoding a large number of coded character variables into dummy variables.  I am using one of the published routines that creates a series of If-Then statements and then stores then in a character variable in a SAS dataset.  Unfortunately, that routine did not indicate how to use the character variable to create executable SAS datastep statements.

Here's an example:

Dataset work.dummy code...

Obs             SAS_Code_Var

1               If orig_var eq 'a'  Then orig_var_a =1;  Else orig_var_a =0;  

2              If orig_var eq 'b'  Then orig_var_b =1;  Else orig_var_b =0;

What is a good way to use this variable to implement executable SAS code?

6 REPLIES 6
Reeza
Super User

is this a one time change or being implemented as part of larger process.

if its a one time change, i'd proc print the data set and copy and paste it into my SAS code.

otherwise i think you need to go into macro coding using %put to create a program and then %include it.

BurntDirt
Calcite | Level 5

Try

data dummy;
    set dummy;

    call execute(SAS_Code_Var);
run;

SASKiwi
PROC Star

If you want a shorter solution: orig_var_a = (orig_var eq 'a'); will give you the same result.

Tom
Super User Tom
Super User

The easiest way is to use a a data step to write a program and then %INCLUDE the generated code.  You can also use CALL EXECUTE to generate the program, but that is much harder to debug since you do not have a physical file that you examine for mistakes.

Note that you cannot just execute an IF statement, it has to be part of a data step.

filename code temp;

data _null_;

  set work.dummy ;

  file code ;

  put SAS_code_var;

run;

data want ;

set have ;

%inc code / source2 ;

run;

Reeza
Super User

Depending on how this code is generated (i.e. a macro) perhaps you could embed that directly in a data step?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

To be honest, if your code is just a series of if statements over a set of variables why not do:

proc sql;

     select distinct NAME

     into :LIST

     from SASHELP.VCOLUMN

     where LIBNAME="WORK" and MEMNAME="ABC";

     select count(distinct NAME)

     into   :TOT

     from  SASHELP.VCOLUMN

     where LIBNAME="WORK" and MEMNAME="ABC";

quit;

data want;

     set have;

     array to_process{&TO_PROCESS.} $20. (&LIST.);

     array done{&TO_PROCESS.} 8.;

     do I=1 to &TO_PROCESS.;

          select(to_process{I});

               when ('a') done{I}=1;

               when ('b') done{I}=0;

               otherwise;

          end;

     end;

run;

Note that this will give you a series of variables DONEx, having the numeric suffix after variable names is easier to deal with when talking about processing multiple columns (i.e. your example would be far easier if your variables were ORIG_VAR_x, then you would just define a range.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 1012 views
  • 0 likes
  • 6 in conversation