Hello,
I'm having trouble creating an ID variable because there are no other variables in the data set for me to use the first.variable trick to create an ID variable. Here is a description of the data. The labels Response_1 through Response_N represent repeated measurements for individuals. I would like to create for the first 12 lines, ID = 1 and for the next 12 lines, ID = 2 all the way until ID = 228. The variable Iter represents the iteration number for this simulated data.
Please let me know if there's anything I can do to clarify what I did. Thank you
-Tim
Keep your own counter per ITER instead of use the automatic _N_ variable.
row+1;
if first.ITER then row=1;
Or you can use a DO Loop around the SET statement, note that means you need to add an OUTPUT statement.
data long2;
do row=1 by 1 until(last.Iter);
set long1;
by Iter;
SUBID = 1 + int((ROW-1)/12);
output;
end;
run;
Or better just use two DO loops.
data long2;
do SUBJID=1 by 1 until(last.iter);
do row=1 to 12 until(last.Iter);
set long1;
by Iter;
output;
end;
end;
run;
Can't you just use arithmetic to make the new variable?
data want ;
id = 1+int((_n_-1)/12);
set have ;
run;
This works! Thank you so much Tom. One more question. Per each iteration, I have 228 subjects. The code you provided works for the first iteration but for Iter = 2, as you can see in the picture, SUBID starts back at 1, which I would like, but it does not repeat the same procedure as for Iter = 1. Here is the code I used to generate the data:
data long2;
set long1;
SUBID = 1 + int((_n_-1)/12);
by Iter;
if first.Iter then SUBID = 1;
run;
Keep your own counter per ITER instead of use the automatic _N_ variable.
row+1;
if first.ITER then row=1;
Or you can use a DO Loop around the SET statement, note that means you need to add an OUTPUT statement.
data long2;
do row=1 by 1 until(last.Iter);
set long1;
by Iter;
SUBID = 1 + int((ROW-1)/12);
output;
end;
run;
Or better just use two DO loops.
data long2;
do SUBJID=1 by 1 until(last.iter);
do row=1 to 12 until(last.Iter);
set long1;
by Iter;
output;
end;
end;
run;
I'm not quite sure I completly get what you are trying to say - Are you saying you want an the same ID for the first 12 Lines and after that keep increasing the id - if so:
data work.aa1;
set your_input;
if _n_ lt 13 then id eq 1;
else id = id + 1;
retain id;
run;
If you always have 12 responses than you could to something like this - using the mod function
data work.aa2;
set your_input;
if _n_ eq 1 then id eq 1;
else if mod(_n_,13) eq 0 then id = id + 1;
retain id;
run;
Or if you need a new id every line
data work.aa3;
set your_input;
id = _n_;
run;
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.