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

Hello guys,

hope you all are doing well.

Here is one task i need your help,

 

I am creating one dataset below,

 

data a;

input subjid cd$ d;

datalines;

2006 XY8 1

2006 XY8 1

2006 ABC 0

2006 EFG 0

2006 HIJ 0

2007 XY8 1

2008 ABC 0

2008 EFG 0

2008 HIJ 0

;

run;

so the query is, if CD variable contents XY8 value then i want to flag that whole subject. for more clear understanding  please look at below table. 

I want output as below,

Desired output:

SUBJID      CD       D   FLG          

2006       XY8         1     Y          

2006       XY8         1     Y          

2006       ABC        0     Y          

2006       EFG        0     Y          

2006       HIJ          0     Y          

2007       XY8        1     Y          

2008       ABC        0

2008       EFG        0

2008       HIJ          0

 

 

 Thanks In Advance... 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here is an SQL approach

 

data a;
input subjid cd$ d;
datalines;
2006 XY8 1
2006 XY8 1
2006 ABC 0
2006 EFG 0
2006 HIJ 0
2007 XY8 1
2008 ABC 0
2008 EFG 0
2008 HIJ 0
;
run;

proc sql;
    create table want as
    select *, sum(cd='XY8')>0 as flg
    from a
    group by subjid;
quit;

View solution in original post

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Here is an SQL approach

 

data a;
input subjid cd$ d;
datalines;
2006 XY8 1
2006 XY8 1
2006 ABC 0
2006 EFG 0
2006 HIJ 0
2007 XY8 1
2008 ABC 0
2008 EFG 0
2008 HIJ 0
;
run;

proc sql;
    create table want as
    select *, sum(cd='XY8')>0 as flg
    from a
    group by subjid;
quit;
Soham0707
Obsidian | Level 7
Thank you so much Draycut..
Jagadishkatam
Amethyst | Level 16

You may try

 

data want;
set a;
if cd='XY8' then sort=0 ;
else sort=1;
run;

proc sort data=want;
by subjid sort;
run;

data want2;
set want;
by subjid sort;
retain flag;
if first.subjid  then flag=' ';
if cd='XY8' then flag='Y';
run;
Thanks,
Jag
Soham0707
Obsidian | Level 7
Thank You Jagdishkatam..
andreas_lds
Jade | Level 19

Problems like yours have been asked and answered so many times, that i am wondering why you haven't found something useful by searching the community. The following code is untested:

 

EDIT: i am sure that there where no answers, when i started writing mine, i am also sure, that writing so few lines didn't took three mins.

 

data want;
  set have;
  by subjid;
  length flg $ 1;
  retain flg;
  if first.subjid then flg = ' ';
  if cd = 'XY8' then flg = 'Y';
run;

 

Soham0707
Obsidian | Level 7
Thank you Andreas_Ids
Jagadishkatam
Amethyst | Level 16

Alternatively in a single data step you can try below code

 

data want;
do until(last.subjid);
set a;
by subjid;
if cd='XY8' then flag='Y';
end;
do until(last.subjid);
set a;
by subjid;
output;
end;
run;

 

 

Thanks,
Jag
PeterClemmensen
Tourmaline | Level 20

Alternatively

 

data want;
    do _N_ = 1 by 1 until (last.subjid);
        set a;
        by subjid;
        if cd='XY8' then flg=1;
    end;
    do _N_ = 1 to _N_;
        set a;
        output;
    end;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 2079 views
  • 5 likes
  • 4 in conversation