BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

The below program generated correct output for T1 to T3, but not T4.  Seq for post a3 should never start with 0, seq for post a3 should always start with 1.

 

Anyone can help? Thanks.


data a;
input id $3. team $4. post $4.;
datalines;
101 t1 a2
102 t1 a3
200 t2 a2
201 t2 a3
300 t3 a2
301 t3 a3
302 t3 a3
400 t4 a3
401 t4 a3
;
run;

 

data a;
set a;
by team post;
seq+1;
if first.team then seq=0;
proc sort; by team post seq;
run;

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Switched the order of two lines 🙂

 

data a;
    input id $3. team $4. post $4.;
    datalines;
100 t1 a1
101 t1 a2
102 t1 a3
200 t2 a2
201 t2 a3
300 t3 a2
301 t3 a3
302 t3 a3
400 t4 a3
401 t4 a3
;
run;

data a;
    set a;
    by team post;
    if first.team then seq=0;
    seq+1;
run;

proc sort;
    by team post seq;
run;
scb
Obsidian | Level 7 scb
Obsidian | Level 7

I am sorry that I should have taken the id 100 out.  Post a2 should always start with 0; and post a3 should always start with 1.  T1 to T3 is correct, but I need help for T4.  Thanks.

 

 

data a;
input id $3. team $4. post $4.;
datalines;
101 t1 a2
102 t1 a3
200 t2 a2
201 t2 a3
300 t3 a2
301 t3 a3
302 t3 a3
400 t4 a3
401 t4 a3
;
run;

 

data a;
set a;
by team post;

seq+1;
if first.team then seq=0;
proc sort; by team post seq;
run;

hhinohar
Quartz | Level 8

Hello,

Still the idea of "T1 to T3 is correct, but I need help for T4" isn't clear,
but if following condition is true, here is a program.

1. Post a2 should always start with 0; and post a3 should always start with 1.
2. Condition 1 should always be carried on to entire obs.

 

/* initial data */
data have;
input id $3. team $4. post $4.;
datalines;
101 t1 a2
102 t1 a3
200 t2 a2
201 t2 a3
300 t3 a2
301 t3 a3
302 t3 a3
400 t4 a3
401 t4 a3
;
run;

data want;
	set have;
		by team
		   post;
/* 	retain statement to hold previous value */
	retain seq 0;
	if first.team 
		then 
			seq=0;
	else 
		seq+1;
/* Post a2 should always start with 0; and post a3 should always start with 1. */
/* 	ok case selection for a2 and a3 case	 */
	select (post);
		when ("a2") 
			seq=0;
		when ("a3") 
			seq=1;
	end;
run;
Kurt_Bremser
Super User

Then you need this?

data b;
set a;
by team post;
if first.team
then do;
  if post = "a3"
  then seq = 1;
  else seq = 0;
end;
else seq + 1;
run;
hhinohar
Quartz | Level 8
data have;
input id $3. team $4. post $4.;
datalines;
101 t1 a2
102 t1 a3
200 t2 a2
201 t2 a3
300 t3 a2
301 t3 a3
302 t3 a3
400 t4 a3
401 t4 a3
;
run;

data want;
	set have;
		by team
		   post;
	seq+1;
	if first.team then do;
			if post="a3" then seq=1;
			else seq=0;
		end;
	else if post="a2" then seq=0;
run;

 

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
  • 5 replies
  • 814 views
  • 0 likes
  • 4 in conversation