BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
2 REPLIES 2
data_null__
Jade | Level 19
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]
deleted_user
Not applicable
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

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
  • 2 replies
  • 623 views
  • 0 likes
  • 2 in conversation