BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alexandraIFCT
Calcite | Level 5
Hello,
I'm trying to auto-create the last column. I tried with a retain but didn't succeed. could you help me please?
Thanks in advance
alexandra
1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Please post your example data as a data step, e.g. like this:

 

data have;
  length Patient_id $1;
  informat Treatment_date mmddyy10.; 
  format Treatment_date mmddyy10.;  
  input Patient_id Treatment_date;
cards;
A 1/1/2020  
A 1/1/2020  
A 5/1/2022  
B 1/12/2021  
C 1/25/2020  
C 6/1t/2021  
D 12/31/2021  
D 12/31/2021  
D 5/25/2022  
D 7/10/2022  
D 7/10/2022  
D 10/4/2022  
;run;

I guess (you did not describe the result you wanted, just showed the example) the calculation you want can be done like this (data must be sorted, as is your example data):

 

 

data want;
  set have; 
  by Patient_id Treatment_date;
  if first.Patient_id then
    Treatment_no=1;
  else if first.Treatment_date then
    Treatment_no+1;
run;

All pretty simple. The statement "Treatment_no+1" is a "sum" statement, and is equivalent to

Treatment_no=sum(Treatment_no,1);
retain Treatment_no;

 

View solution in original post

1 REPLY 1
s_lassen
Meteorite | Level 14

Please post your example data as a data step, e.g. like this:

 

data have;
  length Patient_id $1;
  informat Treatment_date mmddyy10.; 
  format Treatment_date mmddyy10.;  
  input Patient_id Treatment_date;
cards;
A 1/1/2020  
A 1/1/2020  
A 5/1/2022  
B 1/12/2021  
C 1/25/2020  
C 6/1t/2021  
D 12/31/2021  
D 12/31/2021  
D 5/25/2022  
D 7/10/2022  
D 7/10/2022  
D 10/4/2022  
;run;

I guess (you did not describe the result you wanted, just showed the example) the calculation you want can be done like this (data must be sorted, as is your example data):

 

 

data want;
  set have; 
  by Patient_id Treatment_date;
  if first.Patient_id then
    Treatment_no=1;
  else if first.Treatment_date then
    Treatment_no+1;
run;

All pretty simple. The statement "Treatment_no+1" is a "sum" statement, and is equivalent to

Treatment_no=sum(Treatment_no,1);
retain Treatment_no;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 369 views
  • 1 like
  • 2 in conversation