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;
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;
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;
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;
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;
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.