DATA Step, Macro, Functions and more

Evaluating strings as logical expressions

Reply
N/A
Posts: 0

Evaluating strings as logical expressions

I have a data file of survey responses where each record is a response to a single question by a single respondent. For each record, the respondent creates a pattern 1's and 0's that is stored in variables p1-p10.
I also have a string variable in the file called key that is the key to scoring the pattern of values in p1-p10 as either correct or incorrect.(e.g, string value in key might be "p7 and ( p3 or p8 )") .
So, now I want to score records in the file by creating a new variable called "iscorrect" that is value 1 if the logical expression in the key field resolves to true, value 0 otherwise.
Example: If the key field is "p7 and ( p3 or p8 )" and the value of variables p7 and p8 are 1, then iscorrect is 1.
I've been having difficulty using the macro facility to evaluate the string "key" variable as a logical expression. Any suggestions?
Respected Advisor
Posts: 3,799

Re: Evaluating strings as logical expressions

Posted in reply to deleted_user
I don't know of an elegant way to evaluate the expression in KEY as the data step executes. SAS does not have an EXECUTE (data step code) function, AFAIK.

You could code gen some "wall paper" for example. I hope my example data is suitable to model your process.

[pre]
data test;
input (p1-p10)(1.) +1 key $64.;
id + 1;
cards;
0010011101 p7 and ( p3 or p8 )
0010010101 p7 and ( p3 or p8 )
0010010101 p4 and ( p5 or p8 )
0010010101 p7 and ( p4 or p10)
0010010101 p6 and ( p3 or p8 )
;;;;
run;
proc sort data=test(keep=key) out=keys nodupkey;
by key;
run;

filename FT79F001 temp;
data _null_;
file FT79F001;
set keys;
put +6 'when(' key:$quote66. ') iscorrect = ' key ';';
run;
data iscorrect;
set test;
select(key);
%inc FT79F001 / source2;
otherwise;
end;
run;
proc print;
run;
[/pre]
N/A
Posts: 0

Re: Evaluating strings as logical expressions

Posted in reply to data_null__
Bless you, data _null_!
Your "test" table is precisely the input and your "iscorrect" table is the correct output. (Now I'll just have to step through your "FT79F001" processing to understand how you did it!)
It's great to end the weekend knowing that it can be done.
Thanks again, robin
I
Ask a Question
Discussion stats
  • 2 replies
  • 115 views
  • 0 likes
  • 2 in conversation