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;
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
  • 2 replies
  • 1509 views
  • 0 likes
  • 3 in conversation