By group

Reply
N/A
Posts: 0

By group

data xx;
input usubjid visit result $ 5;
cards;
1 1 a
1 2 a
1 3 n
2 1 n
2 2 n
2 3 n
;
run;

if usubjid having any one of the visit, result=a then that usubjid should have new variable x='a' other wise usubjid should have x='n'.

example:

above data set will be having below output:

usubjid x
1 a
2 n

To get this output please give me some sas code.
N/A
Posts: 0

Re: By group

data xx;
input usubjid visit result $ 5;
cards;
1 1 a
1 2 a
1 3 n
2 1 n
2 2 n
2 3 n
;
run;

proc sort data=xx;
by usubjid result;
run;

data xxxx;
set xx;
by usubjid result;
if first.usubjid and first.result and result='a' then x='a';
else x='n';
keep usubjid x;
if first.usubjid and first.result;
run;


Try this it will work.
Super Contributor
Super Contributor
Posts: 3,174

Re: By group

A more general-purpose code logic would be to use IF FIRST.usubjid THEN DO; END; logic to test initialize a "temporary" SAS variable to the desired no-match condition, and use a RETAIN to keep track of this variable, and if the desired "match" condition is encountered for the "test" variable, set the "temporary" variable to a given value. The, use the IF LAST.usubjid THEN DO; END; logic to reset RESULT to the temporary variable and do an OUTPUT.

This logic flow does not require a particular sort-sequence or "match" value order condition to work properly. The key here is using the RETAIN statement to track a temporary variable in the DATA step (remember to either DROP this var or specify a KEEP statement).

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 2 replies
  • 429 views
  • 0 likes
  • 2 in conversation