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

Is there a was to create a count/sequence var to +1 when encountering a '1'? For example, if I had data that looks like this, I do not want to change any sort order, I just want to increase count +1 every time it encounters a 1;  Help is appreciated!

 

Have Want ORD 2
1 1
2 1
3 1
4 1
5 1
1 2
27 2
28 2
29 2
30 2
1 3
37 3
38 3
39 3
40 3
1 4
57 4
58 4
59 4
60 4

 

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @jenim514 

Here is a way to do that.

Best,

data have;
	input num;
	datalines;
1
2
3
4
5
1
27
28
29
30
1
37
38
39
40
1
57
58
59
60
;
run;

data want;
	set have;
	by num notsorted;
	retain count;
	if num=1 and first.num then count+1;
run;
	

View solution in original post

2 REPLIES 2
ed_sas_member
Meteorite | Level 14

Hi @jenim514 

Here is a way to do that.

Best,

data have;
	input num;
	datalines;
1
2
3
4
5
1
27
28
29
30
1
37
38
39
40
1
57
58
59
60
;
run;

data want;
	set have;
	by num notsorted;
	retain count;
	if num=1 and first.num then count+1;
run;
	
novinosrin
Tourmaline | Level 20

Hi @jenim514  Methinks all you need is

if num=1 then want+1;
data have;
	input num;
	datalines;
1
2
3
4
5
1
27
28
29
30
1
37
38
39
40
1
57
58
59
60
;

data want;
	set have;
	if num=1 then want+1;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 711 views
  • 0 likes
  • 3 in conversation