Hello everyone!
I will be very grateful if someone helps me solve the problem.
I have a table with two columns:
time | marker |
13:22 | 1 |
13:23 | 1 |
13:24 | 1 |
13:25 | 0 |
13:26 | 0 |
13:27 | 0 |
13:28 | 0 |
13:29 | 1 |
13:30 | 1 |
13:31 | 1 |
13:32 | 0 |
13:33 | 0 |
13:34 | 1 |
13:35 | 0 |
13:36 | 1 |
13:37 | 0 |
13:38 | 0 |
I need to add a third column, which would increase every time the marker changes
time | marker | numb |
13:22 | 1 | 1 |
13:23 | 1 | 1 |
13:24 | 1 | 1 |
13:25 | 0 | 2 |
13:26 | 0 | 2 |
13:27 | 0 | 2 |
13:28 | 0 | 2 |
13:29 | 1 | 3 |
13:30 | 1 | 3 |
13:31 | 1 | 3 |
13:32 | 0 | 4 |
13:33 | 0 | 4 |
13:34 | 1 | 5 |
13:35 | 0 | 6 |
13:36 | 1 | 7 |
13:37 | 0 | 8 |
13:38 | 0 | 8 |
And after that, I need to print the FIRST line for each of the groups
time | numb |
13:22 | 1 |
13:25 | 2 |
13:29 | 3 |
… | … |
Perhaps this is really a simple task, but I'm a beginner, and would like to see a detailed code to solve it 🙂
Please try this.
The data step outputs 2 datasets:
- the "full one"
- the one for printing, which select the first observation of each Numb group.
The datastep uses a BY statement (by marker notsorted;
) in order to create the internal variable first.marker, which identifies the first observation of the marker value.
Notsorted is important here to tell SAS that it is normal not to have all values 0 and all value 1 together (ascending order).
Best,
data have;
infile datalines dlm="09"x;
input time:time10. marker;
format time time10.;
datalines;
13:22 1
13:23 1
13:24 1
13:25 0
13:26 0
13:27 0
13:28 0
13:29 1
13:30 1
13:31 1
13:32 0
13:33 0
13:34 1
13:35 0
13:36 1
13:37 0
13:38 0
;
run;
data want_full want_for_print;
set have;
by marker notsorted;
if first.marker then do;
numb+1;
output want_for_print;
end;
output want_full;
run;
proc print data=want_for_print;
var time numb;
run;
Please try this.
The data step outputs 2 datasets:
- the "full one"
- the one for printing, which select the first observation of each Numb group.
The datastep uses a BY statement (by marker notsorted;
) in order to create the internal variable first.marker, which identifies the first observation of the marker value.
Notsorted is important here to tell SAS that it is normal not to have all values 0 and all value 1 together (ascending order).
Best,
data have;
infile datalines dlm="09"x;
input time:time10. marker;
format time time10.;
datalines;
13:22 1
13:23 1
13:24 1
13:25 0
13:26 0
13:27 0
13:28 0
13:29 1
13:30 1
13:31 1
13:32 0
13:33 0
13:34 1
13:35 0
13:36 1
13:37 0
13:38 0
;
run;
data want_full want_for_print;
set have;
by marker notsorted;
if first.marker then do;
numb+1;
output want_for_print;
end;
output want_full;
run;
proc print data=want_for_print;
var time numb;
run;
You're welcome 😊
I have adapted the code so that you can get the new column:
data want;
set have;
format time time2 time10.;
by marker notsorted;
retain time2;
numb_str+1;
if first.marker then do;
numb+1;
time2=time;
numb_str=1;
end;
if last.marker then output;
drop time marker;
rename time2 = time;
run;
proc print data=want;
run;
Best,
please try the below code
data have;
input time :time5. marker;
cards;
13:22 1
13:23 1
13:24 1
13:25 0
13:26 0
13:27 0
13:28 0
13:29 1
13:30 1
13:31 1
13:32 0
13:33 0
13:34 1
13:35 0
13:36 1
13:37 0
13:38 0
;
data want;
set have;
by marker notsorted;
retain numb;
if first.marker then numb+1;
if first.marker;
format time time5.;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.