BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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
ed_sas_member
Meteorite | Level 14

HI @olgazabelinasas 

 

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;

 

View solution in original post

6 REPLIES 6
ed_sas_member
Meteorite | Level 14

HI @olgazabelinasas 

 

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;

 

olgazabelinasas
SAS Employee
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
… … …
ed_sas_member
Meteorite | Level 14

Hi @olgazabelinasas 

 

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;

Capture d’écran 2020-05-13 à 09.38.23.png

Best, 

olgazabelinasas
SAS Employee
Thank you so much again!
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
olgazabelinasas
SAS Employee
Thank you very much!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 553 views
  • 3 likes
  • 3 in conversation