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 |
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;
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;
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;
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.
Ready to level-up your skills? Choose your own adventure.