Hi
I tried to make an index that assign number 1 for each sequence start in the table below. I try this with the following code:
But the problem is that i Want to have a new unique number for index when the sequence variable (seqn) start in the beginning again. Like 1 for seqn 1 , 2, and index=2 for seqn 1 until 14 in this example?
How can I solve this example?
I do understand.
Here are my results from the code above. I think that is what you describe?
ID seqn index 101 1 1 101 2 1 101 3 1 101 1 2 101 2 2 102 1 1 102 2 1 102 3 1 102 4 1 102 1 2 102 2 2
I can't tell what you want the output to look like.
Note: we can't code against pictures.
It is best to provide data in the form of a data step. There are hundreds of examples on the forum for such.
I think this is what you want
data have;
do seqnr = 1, 2, 1 to 14;
output;
end;
run;
data want;
set have;
if seqnr = 1 then index + 1;
run;
Do the individuals have IDs? As I understand it, you want to reset the index to 1 whenever seqnr is 1. Which is that my code does.
Like this?
data have;
input ID seqn;
datalines;
101 1
101 2
101 3
101 1
101 2
102 1
102 2
102 3
102 4
102 1
102 2
;
data want;
set have;
by ID;
if first.ID then index = 0;
if seqn = 1 then index + 1;
run;
I do understand.
Here are my results from the code above. I think that is what you describe?
ID seqn index 101 1 1 101 2 1 101 3 1 101 1 2 101 2 2 102 1 1 102 2 1 102 3 1 102 4 1 102 1 2 102 2 2
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.