DATA Step, Macro, Functions and more

Assigning number to certain subjects

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

Assigning number to certain subjects

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;

 


Accepted Solutions
Solution
Wednesday
PROC Star
Posts: 263

Re: Assigning number to certain subjects

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


All Replies
PROC Star
Posts: 1,778

Re: Assigning number to certain subjects


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;
Regular Contributor
Posts: 156

Re: Assigning number to certain subjects

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

 

?

--------------
blog: papersandprograms.com
PROC Star
Posts: 1,778

Re: Assigning number to certain subjects

Posted in reply to PaulBrownPhD

@PaulBrownPhD Neat!!!!!

Trusted Advisor
Posts: 1,337

Re: Assigning number to certain subjects

Posted in reply to PaulBrownPhD

@PaulBrownPhD   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;

 

Solution
Wednesday
PROC Star
Posts: 263

Re: Assigning number to certain subjects

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;

 

Occasional Contributor
Posts: 18

Re: Assigning number to certain subjects

Thank you this solution works too. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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