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

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. Screen Shot 2018-10-16 at 8.34.19 PM.png

Please let me know if there's anything I can do to clarify what I did. Thank you

 

-Tim

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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; 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Can't you just use arithmetic to make the new variable?

data want ;
  id = 1+int((_n_-1)/12);
  set have ;
run;
  
tbanh
Fluorite | Level 6

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;

Screen Shot 2018-10-17 at 10.33.22 AM.png

 

 

 

Tom
Super User Tom
Super User

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; 
Criptic
Lapis Lazuli | Level 10

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; 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 928 views
  • 2 likes
  • 3 in conversation