DATA Step, Macro, Functions and more

Make SAS interpret code contained in variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Make SAS interpret code contained in variable

Hi,

I have made the following small dataset in order to illustrate my problem:

screen_dump_dataset.png

What I need to do is to write a program that from columns 'algorithm' and 'category' calculates column 'result'. I have no idea where to start or if this is even possible in SAS. All tips and ideas are welcome.

/T


Accepted Solutions
Solution
‎07-16-2012 11:38 PM
Super User
Posts: 10,044

Re: Make SAS interpret code contained in variable

Very interesting thing.

data have;
input id algorithm :&$10. category :$8.;
cards;
1 B and C   ABD
2 B and C   ABC
3 C or D    ABC
4 C or D    AB 
;
run;
data want(drop=i temp _algorithm);
 set have;
 length temp  $ 4 result $ 5;
 _algorithm=algorithm ;
 do i=1 to countw(_algorithm);
  temp=scan(_algorithm,i);
  if length(temp)=1 then do;
                          if find(category,strip(temp),'i') then _algorithm=translate(_algorithm,'1',temp);
                           else _algorithm=translate(_algorithm,'0',temp);
                         end;
 end;
 result=ifc( resolve(cats('%sysevalf(',_algorithm,')'))='0','FALSE','TRUE')  ;
run;


Ksharp

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: Make SAS interpret code contained in variable

Well, the following only work on single character evaluation:

data have;

input id algorithm :&$10. category :$8.;

cards;

1 B and C   ABD

2 B and C   ABC

3 C or D    ABC

4 C or D    AB 

;

data want;

set have;

length result $8.;

if findw(algorithm,'and')>0 then do;

_t=    compress(algorithm,' and');

do _i=1 to lengthn(_t);

   _ts=substr(_t,_i,1);

   if  findc(category,strip(_ts))=0 then do;

             result='FALSE';

             OUTPUT;

             return;

    end;

end;

result='TRUE';

end;

else if findw(algorithm,'or')>0 then do;

_t=    compress(algorithm,' or');

do _i=1 to lengthn(_t);

   _ts=substr(_t,_i,1);

    if findc(category,strip(_ts))>0 then do;

             result='TRUE';

             OUTPUT;

             return;

    end;

end;

result='FALSE';

END;

OUTPUT;

drop _:;

RUN;

proc print;run;

Haikuo

Respected Advisor
Posts: 3,156

Re: Make SAS interpret code contained in variable

An update supposedly working on multiple letter evaluation:

data have;

input id algorithm :&$10. category :$8.;

cards;

1 B and C   ABD

2 B and C   ABC

3 C or D    ABC

4 C or D    AB 

5 BC or DA  ABCDE

6 BC and DA  ABCDE

;

data want;

set have;

length result $8.;

if findw(algorithm,'and')>0 then do;

_t=    compress(algorithm,'and');

do _i=1 to countw(_t);

   _ts=scan(_t,_i);

   if  find(category,strip(_ts))=0 then do;

             result='FALSE';

             OUTPUT;

             return;

    end;

end;

result='TRUE';

end;

else if findw(algorithm,'or')>0 then do;

_t=    compress(algorithm,'or');

do _i=1 to countw(_t);

   _ts=scan(_t,_i);

    if findc(category,strip(_ts))>0 then do;

             result='TRUE';

             OUTPUT;

             return;

    end;

end;

result='FALSE';

END;

OUTPUT;

drop _:;

RUN;

proc print;run;

Haikuo

Regular Contributor
Posts: 241

Re: Make SAS interpret code contained in variable

There are many ways to do something like this. Here is one way.

ods _all_ close;
ods listing;
options nocenter;
 
/* a test data */
data one;
  infile cards firstobs=2 missover;
  input cmd $ 1-6 var $ 8-14 parm1 $ 15-25 parm2 $ 26-34;
cards;
----+----1----+----2----+----3----+
rename name   firstname
recode tall   height>65  1
recode tall   height<=65 0
;
run;
 
filename cmds temp;
  /* generate sas code from dataset one to a temporary file */
  data _null_;
    length line $200;
    file cmds;
    set one;
    select(cmd);
      when("rename") line = "rename #var# = #parm1#;";
      when("recode") line = "if #parm1# then #var# = #parm2#;";
      otherwise;
    end;
    line = tranwrd(line, "#var#", trimn(var));
    line = tranwrd(line, "#parm1#", trimn(parm1));
    line = tranwrd(line, "#parm2#", trimn(parm2));
    put line;
  run;
  /* run the generated code */
  data class;
    set sashelp.class;
    %inc cmds/source2;
  run;
filename cmds clear;
 
/* check */
proc print data=class;
run;
/* on lst
Obs    firstname    Sex    Age    Height    Weight    tall
  1     Alfred       M      14     69.0      112.5      1
  2     Alice        F      13     56.5       84.0      0
  3     Barbara      F      13     65.3       98.0      1

  ...
*/

Solution
‎07-16-2012 11:38 PM
Super User
Posts: 10,044

Re: Make SAS interpret code contained in variable

Very interesting thing.

data have;
input id algorithm :&$10. category :$8.;
cards;
1 B and C   ABD
2 B and C   ABC
3 C or D    ABC
4 C or D    AB 
;
run;
data want(drop=i temp _algorithm);
 set have;
 length temp  $ 4 result $ 5;
 _algorithm=algorithm ;
 do i=1 to countw(_algorithm);
  temp=scan(_algorithm,i);
  if length(temp)=1 then do;
                          if find(category,strip(temp),'i') then _algorithm=translate(_algorithm,'1',temp);
                           else _algorithm=translate(_algorithm,'0',temp);
                         end;
 end;
 result=ifc( resolve(cats('%sysevalf(',_algorithm,')'))='0','FALSE','TRUE')  ;
run;


Ksharp

Occasional Contributor
Posts: 8

Re: Make SAS interpret code contained in variable

Wounderful, beutiful, thank you one million.

Had to add follwing to your code because in the real dataset letters can appear several times in the 'category' variable and the second time the loop found the same letter it went a bit bananas.

if length(temp)=1 and temp ne '1' and temp ne '0' then do;

Otherwise I can use the code as is. Perfect.

/T

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 270 views
  • 1 like
  • 4 in conversation