BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TTNY
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

5 REPLIES 5
Haikuo
Onyx | Level 15

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

Haikuo
Onyx | Level 15

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

chang_y_chung_hotmail_com
Obsidian | Level 7

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

  ...
*/

Ksharp
Super User

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

TTNY
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 953 views
  • 1 like
  • 4 in conversation