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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 6 replies
  • 1349 views
  • 3 likes
  • 5 in conversation