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

Hi All,

 

Could anyone help me with the below requirement?

 

I have 3 variables SUBJ CODE TRT  which have values as following.

 

SUBJ     CODE   TRT

1001        AB          A

1001        AB          B

1002       CD          C

1002       CD          C

1003       EF           E

1003       EF           F

 

In the above data for each SUBJ there is a specific code which have corresponding TRT vales as A and B. Example, for SUBJ 1001, code is AB and TRT is having 2 records for same SUBJ and CODE with A in 1 row and B in next row. If it is 1002 then the 2 TRT records should be C and D, but if it C and C then it should be flagged. See below expectation.

 

SUBJ     CODE   TRT  FLAG

1001        AB          A       0

1001        AB          B        0

1002       CD          C        0

1002       CD          C         1

1003       EF           E        0

1003       EF           F         0

1004       AD          A        0

1004       AD           A        1

 

Can anyone help me achieve this?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data have;
input SUBJ $ CODE $ TRT $;
datalines;
1001 AB A
1001 AB B
1002 CD C
1002 CD C
1003 EF E
1003 EF F
1004 AD A
1004 AD A
;

data want;
set have;
by subj code trt notsorted;
if
  first.trt ne last.trt /* indicates consecutive observations with same value */
  and last.trt /* we want to flag only the second occurence */
then flag = 1;
else flag = 0;
run;

proc print data=want noobs;
run;

Result:

SUBJ	CODE	TRT	flag
1001	AB	A	0
1001	AB	B	0
1002	CD	C	0
1002	CD	C	1
1003	EF	E	0
1003	EF	F	0
1004	AD	A	0
1004	AD	A	1

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User
data have;
input SUBJ $ CODE $ TRT $;
datalines;
1001 AB A
1001 AB B
1002 CD C
1002 CD C
1003 EF E
1003 EF F
1004 AD A
1004 AD A
;

data want;
set have;
by subj code trt notsorted;
if
  first.trt ne last.trt /* indicates consecutive observations with same value */
  and last.trt /* we want to flag only the second occurence */
then flag = 1;
else flag = 0;
run;

proc print data=want noobs;
run;

Result:

SUBJ	CODE	TRT	flag
1001	AB	A	0
1001	AB	B	0
1002	CD	C	0
1002	CD	C	1
1003	EF	E	0
1003	EF	F	0
1004	AD	A	0
1004	AD	A	1
r3570
Obsidian | Level 7

@Kurt_Bremser 

 

Thanks for your program. I would like to make this program as dynamic like if the CODE variable has ABC instead of AB and TRT should have 3 records with A, B, C then how can we modify the program?

Kurt_Bremser
Super User

The BY statement does not care about the length of variables. As long as there are two (or more) identical TRT values following each other within a SUBJ/CODE group, the flag will be set.

If there's more to it, you have to supply additional example data (see how it's done in a DATA step), and the expected result from this.

r3570
Obsidian | Level 7

Lets say my data is like this

 

SUBJ     CODE   TRT

1001        ABC         A

1001        ABC          B

1001        ABC          C

1002       CDE          C

1002       CDE          C

1002       CDE          E

1003       EFG          E

1003       EFG          F

1003       EFG          F

 

I NEED output like this

SUBJ     CODE   TRT      FLAG

1001        ABC         A           0

1001        ABC          B          0

1001        ABC          C          0

1002       CDE          C          0

1002       CDE          C          1

1002       CDE          E          0

1003       EFG          E          0

1003       EFG          F          0

1003       EFG          F           1

Kurt_Bremser
Super User

The same code works:

data have;
input SUBJ $ CODE $ TRT $;
datalines;
1001 ABC A
1001 ABC B
1001 ABC C
1002 CDE C
1002 CDE C
1002 CDE E
1003 EFG E
1003 EFG F
1003 EFG F
;

data want;
set have;
by subj code trt notsorted;
if
  first.trt ne last.trt /* indicates consecutive observations with same value */
  and last.trt /* we want to flag only the second occurence */
then flag = 1;
else flag = 0;
run;

proc print data=want noobs;
run;

Result:

SUBJ	CODE	TRT	flag
1001	ABC	A	0
1001	ABC	B	0
1001	ABC	C	0
1002	CDE	C	0
1002	CDE	C	1
1002	CDE	E	0
1003	EFG	E	0
1003	EFG	F	0
1003	EFG	F	1
Ksharp
Super User
data have;
input SUBJ $ CODE $ TRT $;
datalines;
1001 ABC A
1001 ABC B
1001 ABC C
1002 CDE C
1002 CDE C
1002 CDE E
1003 EFG E
1003 EFG F
1003 EFG F
;
data want;
 do until(last.subj);
  set have;
  by subj;
  length temp $ 80;
  temp=cats(temp,trt);
 end;
 do i=1 by 1 until(last.subj);
  set have;
  by subj;
  flag=ifn(char(code,i)=char(temp,i),0,1);
  output;
 end;
drop temp i;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 6 replies
  • 1098 views
  • 1 like
  • 3 in conversation