SAS Employee

## HELP with simple code :)

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 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Meteorite | Level 14

## Re: HELP with simple code :)

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;``````

6 REPLIES 6
Meteorite | Level 14

## Re: HELP with simple code :)

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;``````

SAS Employee

## Re: HELP with simple code :)

Thank you so much!

and the last question: how to add the number of rows in each group to the last table?

time numb numb_str
13:22 1 3
13:25 2 4
13:29 3 3
… … …
Meteorite | Level 14

## Re: HELP with simple code :)

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,

SAS Employee

## Re: HELP with simple code :)

Thank you so much again!
Amethyst | Level 16

## Re: HELP with simple 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;
``````
Thanks,
Jag
SAS Employee

## Re: HELP with simple code :)

Thank you very much!
Discussion stats
• 6 replies
• 553 views
• 3 likes
• 3 in conversation