Hi,
I have made the following small dataset in order to illustrate my problem:
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
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
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
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
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
...
*/
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
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
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.