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

Hello all,

 

I am attempting to differentiate between the first variable in a group and the second. Below is an example of what I want to do. I have the ID variable and I have the type variable. I need the First variable. In my data CC and RC go together and I need to tell the difference between the first one in the group and the second. So far I've looked into last. and first. but that didn't look like it would fit my needs. I don't care about the R and T type at the moment. I don't mind if it doesn't look exactly like the example as long as I can tell the difference between the first group and last. Thanks

 

 

ID Type First
1 r  
2 r  
3 t  
4 t  
5 cc 1
6 cc 0
7 r  
8 t  
9 rr 1
10 rr 0
11 t  
12 t  
13 t  
14 cc 1
15 cc 0

 

data temp;
input ID type $;
datalines;
1 r
2 r
3 r
4 t
5 CC
6 CC
7 r
8 r
9 RR
10 RR
11 r
12 t
13 t
14 CC
15 CC
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

I would define the groups first and then you can use BY group processing on the new group variable.

data groups;
  set temp;
  group+(lag(type) ne type)or(lag(id)+1 ne id);
run;
data want ;
  set groups;
  by group;
  if type in ('CC','RR') then do;
     if first.group and last.group then first='ERROR';
     else if first.group then first='1';
     else first='0';
  end;
run;

Capture.PNG

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

This delivers your intended result:

data temp;
input ID type $;
datalines;
1 r
2 r
3 r
4 t
5 CC
6 CC
7 r
8 r
9 RR
10 RR
11 r
12 t
13 t
14 CC
15 CC
;
run;

data want;
set temp;
by type notsorted;
if type in ('CC','RR') then first = first.type;
run;
michelconn
Quartz | Level 8

Thanks a lot, that almost has the desired affect. There are a few things that make the data a little tricky I noticed after scanning through the results on my main dataset. I need to differentiate between types that don't have a sequential ID and indicate an error for types that aren't grouped with a like type. 

 

data temp;
input ID type $;
datalines;
1 r
2 r
3 r
4 t
5 CC
6 CC
10 CC
11 CC
12 r
19 r
20 RR
21 RR
30 r
32 t
39 t
41 CC
50 t
56 r
;
run;

 

 

ID  TYPE FIRST
1 r  
2 r  
3 r  
4 t  
5 CC 1
6 CC 0
10 CC 1
11 CC 0
12 r  
19 r  
20 RR 1
21 RR 0
30 r  
32 t  
39 t  
41 CC Need to indicate this is an error
50 t  
56 r  



 

Tom
Super User Tom
Super User

I do not see a pattern in the FIRST column. You need to do a better job of explaining what you want.

If you want to treat changes in the value of TYPE as indicating the beginning of a new group then you can use BY processing. So the first three records all have type='r'. Then there is one record with type='t'. etc.

 

The data does not need to be sorted since you can use the NOTSORTED keyword on the BY statement.

data want;
  set temp;
  by type notsorted;
  groupno+first.type;
  if first.type then recno=0;
  recno+1;
run;

Capture.PNG

 

 

michelconn
Quartz | Level 8

Hopefully this makes more sense. The ID variable has to be sequential for the TYPE to be in the same group. So the ID 5 and 6 need to be grouped together but are not in the same group as 10 and 11. I don't really need the GROUP variable but hopefully it makes things more clear. 

 

ID  TYPE group FIRST
1 r    
2 r    
3 r    
4 t    
5 CC 1 1
6 CC 1 0
10 CC 2 1
11 CC 2 0
12 r    
19 r    
21 RR 3 1
21 RR 3 0
30 r    
32 t    
39 t    
41 CC error Need to indicate this is an error
50 t    
56 r    

 

Tom
Super User Tom
Super User

I would define the groups first and then you can use BY group processing on the new group variable.

data groups;
  set temp;
  group+(lag(type) ne type)or(lag(id)+1 ne id);
run;
data want ;
  set groups;
  by group;
  if type in ('CC','RR') then do;
     if first.group and last.group then first='ERROR';
     else if first.group then first='1';
     else first='0';
  end;
run;

Capture.PNG

michelconn
Quartz | Level 8

Thanks! This got me to a solution.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1187 views
  • 2 likes
  • 3 in conversation