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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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