BookmarkSubscribeRSS Feed
yahoo0806
Calcite | Level 5

Hi all,

Would you mind let me know what your thoughts are if I want to count for below dataset: column id, order, and type are the dataset I currently have, and column count is the dataset I want to create. Basically, I want to count the type variable within each id following the order, i.e., if the one type got interrupted by another type, then the second part of the same type need to be re-counted for this id. 

idordertypecount
11Not_Applicable1
12Not_Applicable2
13Not_Applicable3
14Not_Applicable4
15Not_Applicable5
16Not_Applicable6
17Not_Applicable7
18Not_Applicable8
19Not_Applicable9
110Not_Applicable10
111Not_Applicable11
112Not_Applicable12
113Not_Applicable13
114Not_Applicable14
115Not_Applicable15
116Not_Applicable16
117Not_Applicable17
118Not_Applicable18
119intermittent1
120Not_Applicable1
21Not_Applicable1
22Not_Applicable2
23Not_Applicable3
24Not_Applicable4
25Not_Applicable5
26Not_Applicable6
27Not_Applicable7
28Not_Applicable8
29Not_Applicable9
210Not_Applicable10
211Not_Applicable11
212Not_Applicable12
213Not_Applicable13
214Discontinued1
215Discontinued2
216Discontinued3
217Discontinued4
218Discontinued5
219Discontinued6
220Discontinued7
31Not_Applicable1
32Not_Applicable2
33Not_Applicable3
34Not_Applicable4
35Not_Applicable5
36Not_Applicable6
37Not_Applicable7
38Not_Applicable8
39Not_Applicable9
310Not_Applicable10
311intermittent1
312intermittent2
313intermittent3
314intermittent4
315intermittent5
316Not_Applicable1
317Not_Applicable2
318Not_Applicable3
319Not_Applicable4
320Not_Applicable5
4 REPLIES 4
ballardw
Super User

See if this come close:

data want;
   set have;
   by id order type notsorted;
   retain count;
   if first.id or first.type the count=1;
   else count+1;
run;
yahoo0806
Calcite | Level 5

no, it's not working, i got count  = 1 for every row.

 

ballardw
Super User

@yahoo0806 wrote:

no, it's not working, i got count  = 1 for every row.

 


Try with out ORDER on the BY statement.

 

Couldn't test the code as no data step supplied and too lazy at the moment to turn your "data" into one.

PeterClemmensen
Tourmaline | Level 20

This should do it.

 

data have;
input id order type $20.;
datalines;
1 1  Not_Applicable 
1 2  Not_Applicable 
1 3  Not_Applicable 
1 4  Not_Applicable 
1 5  Not_Applicable 
1 6  Not_Applicable 
1 7  Not_Applicable 
1 8  Not_Applicable 
1 9  Not_Applicable 
1 10 Not_Applicable 
1 11 Not_Applicable 
1 12 Not_Applicable 
1 13 Not_Applicable 
1 14 Not_Applicable 
1 15 Not_Applicable 
1 16 Not_Applicable 
1 17 Not_Applicable 
1 18 Not_Applicable 
1 19 intermittent   
1 20 Not_Applicable 
2 1  Not_Applicable 
2 2  Not_Applicable 
2 3  Not_Applicable 
2 4  Not_Applicable 
2 5  Not_Applicable 
2 6  Not_Applicable 
2 7  Not_Applicable 
2 8  Not_Applicable 
2 9  Not_Applicable 
2 10 Not_Applicable 
2 11 Not_Applicable 
2 12 Not_Applicable 
2 13 Not_Applicable 
2 14 Discontinued   
2 15 Discontinued   
2 16 Discontinued 
2 17 Discontinued 
2 18 Discontinued 
2 19 Discontinued 
2 20 Discontinued 
3 1  Not_Applicable 
3 2  Not_Applicable 
3 3  Not_Applicable 
3 4  Not_Applicable 
3 5  Not_Applicable 
3 6  Not_Applicable 
3 7  Not_Applicable 
3 8  Not_Applicable 
3 9  Not_Applicable 
3 10 Not_Applicable 
3 11 intermittent   
3 12 intermittent   
3 13 intermittent   
3 14 intermittent   
3 15 intermittent   
3 16 Not_Applicable 
3 17 Not_Applicable 
3 18 Not_Applicable 
3 19 Not_Applicable 
3 20 Not_Applicable 
;

data want;
   set have;
   by id type notsorted;
   if first.type then count = 0;
   count + 1;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 2144 views
  • 4 likes
  • 3 in conversation