BookmarkSubscribeRSS Feed
himself
Pyrite | Level 9

Hi, 

I need help in the following by group processing, such for the same USUBJID, and have AVAL=0 then i need the final output have flag='Y', if that is not the case then populate flag='N'.

himself_0-1620384528220.png

data create;
input usubjid $ aval;
cards;
12 1
12 4
12 0
12 2
13 2
13 5
13 6
13 4
13 0
14 5
14 7
14 8
;
run;

proc sort data=create;
  by usubjid aval;
run;

data check;
  set create;
  by usubjid aval;
	if first.usubjid and aval=0 then flag="Y";
run;



3 REPLIES 3
PaigeMiller
Diamond | Level 26

This can be done using a RETAIN statement

 

data check;
    set create;
    by usubjid aval;
    if first.usubjid and aval=0 then flagg="Y"; 
    else flagg="N";
    retain flag;
    if first.usubjid then flag=flagg;
    drop flagg;
run;
--
Paige Miller
himself
Pyrite | Level 9
Hi @PaigeMiller, thanks for the feedback, At some point i thought, the following will work fine,
data check1;
set create;
by usubjid aval;
retain flag;
if first.usubjid and aval=0 then flag="Y";
else flag="N";
run;

But what i have realised is that one must "if first.usubjid then flag=flagg;"
FreelanceReinh
Jade | Level 19

Hi @himself,


@himself wrote:
But what i have realised is that one must "if first.usubjid then flag=flagg;"

A second variable (flagg) can be used, but is not necessary:

data check;
set create;
by usubjid;
retain flag ' ';
if first.usubjid then flag=ifc(aval=0,'Y','N');
run;

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1041 views
  • 1 like
  • 3 in conversation