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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.