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

I want the variable 'change' to be set to 1 for first 3  patients, 2 for second 3 patients and so on.

 

 

data have;

   input subject change;

datalines;

12345   1

12345   1 

12345    1


23456    1

23456    1

23456    1

23456     1

34567     1


34567   1
34567   1
34567   1
45678   2  
45678   2
45678   2
45678   2


run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Instead of creating a variable to count the number of subjects read, you can loop for each subject, so that you can use the _N_ variable:

data want;
  change+mod(_N_,3)=1;
  do until(last.subject);
    set have;
    by subject;
    output;
    end;
run;

 

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

data have;

   input subject ;

datalines;
12345   1
12345   1 
12345    1
23456    1
23456    1
23456    1
23456     1
34567     1
34567   1
34567   1
34567   1
45678   2  
45678   2
45678   2
45678   2
run;
 
data want;
set have;
by subject;
retain change 1;
if first.subject then patient+1;
if mod(patient,3)=0 and last.subject then do; output;change+1;end;
else output;
drop patient;
run;
pau13rown
Lapis Lazuli | Level 10

data xxx;
  retain count id 0 ;
  set yyy;
  count=count+1;
  if mod(count,3)=1 then id=id+1;
run;

 

?

novinosrin
Tourmaline | Level 20

@pau13rown Neat!!!!!

mkeintz
PROC Star

@pau13rown   You're increenting count with every observation, but I think you intend to increment only with every new subject:

 

data want;
  set have;
  by subject;
  if first.subject then do;
    count+1;
    id+(mod(count,3)=1);
  end;
run;

And one can even get away without creating the count variable, via a conditional use of lag.  I don't particularly recommend it, but it's a good demonstration that lag functions don't do "look-backs", but rather they manage queues:

 

data want;
  set have;
  by subject;
  retain id 1;
  if first.subject then id+(lag3(id)=id);
run;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
s_lassen
Meteorite | Level 14

Instead of creating a variable to count the number of subjects read, you can loop for each subject, so that you can use the _N_ variable:

data want;
  change+mod(_N_,3)=1;
  do until(last.subject);
    set have;
    by subject;
    output;
    end;
run;

 

gpv2000
Calcite | Level 5

Thank you this solution works too. 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 4161 views
  • 3 likes
  • 5 in conversation