BookmarkSubscribeRSS Feed
mfowlk
Calcite | Level 5

I'm trying find a sum for var1 and separate sum for var2 for each row based on the sas date column labelled dt and two days prior (which is in the sum_to_date) within each ID.

 

For example,

ID232445

line 5 sum(var1) = 0 sum(var2) =1

line 6 sum(var1) = 0 sum(var2) =2

line 7 sum(var1) = 0 sum(var2) =3

line 8 sum(var1) = 0 sum(var2) =3 

line 9 sum(var1) = 0 sum(var2) =4

line 10 sum(var1) = 0 sum(var2) =3

line 11 sum(var1) = 0 sum(var2) =1

line 12 sum(var1) =0 sum(var2)=2

continue summing until reaching ID 112344, then begin the summing again.Screenshot 2023-07-05 153753.png

 

 

7 REPLIES 7
Reeza
Super User
Please show expected output.
Quentin
Super User

Can you post this example data as a CARDS data step, for the data you HAVE, along with the data you WANT to create, and also the code you have tried?  Are you familiar with BY-group processing?  Seeing the code you have tried will help others help you. 

 

I don't really understand how DT is involved in your logic.  Seeing the data you HAVE as input and the data you WANT as output will help.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
mfowlk
Calcite | Level 5

Quentin, 

Thank you! The date that I have as input is in the picture of the spreadsheet. I will create what I want in another spreadsheet.

Reeza
Super User
Rather than spreadsheet please post your data as text into the forum directly or better yet, as a data step as I did in my answer. Makes it much easier to respond to you.
mfowlk
Calcite | Level 5

I'm new to this forum. Is this what you mean by 'posting data as text directly into forum'?

 

HAVE      
IDvar1var2dtsum_to_date 
232445012314523143  
232445002314523143  
232445012315023148  
232445002315123149  
232445012315123149  
232445012315123149  
232445002315223150  
232445012315223150  
232445002315323151  
232445012315723155  
232445012315723155  
232445002315823156  
232445002315923157  
232445002315923157  
232445012315923157  
232445002316023158  
112344002313523133  
112344002313523133  
112344012313523133  
112344012313623134  
112344002313623134  
112344012313723135  
112344012313723135  
       
WANT      
IDvar1var2dtsum_to_datesum(var1)sum(var2)
232445012314523143  
232445002314523143  
23244501231502314801
23244500231512314901
23244501231512314902
23244501231512314903
23244500231522315003
23244501231522315004
23244500231532315103
23244501231572315501
23244501231572315502
23244500231582315602
23244500231592315702
23244500231592315702
23244501231592315703
23244500231602315801
11234400231352313300
11234400231352313300
11234401231352313301
11234401231362313402
11234400231362313402
11234401231372313503
11234401231372313504
Reeza
Super User

You have duplicate rows with nothing that uniquely identifies a row making this a bit messier. 

 

This seems to roughly work. Not sure of your logic though as you start the answers at 5 but line 4 should also have an answer as well? Are the end of the intervals included?

 

data have;
infile cards truncover;
input ID $ var1    var2    dt  sum_to_date;
format dt sum_to_date date9.;
cards;
232445  0   1   23145   23143
232445  0   0   23145   23143
232445  0   1   23150   23148
232445  0   0   23151   23149
232445  0   1   23151   23149
232445  0   1   23151   23149
232445  0   0   23152   23150
232445  0   1   23152   23150
232445  0   0   23153   23151
232445  0   1   23157   23155
232445  0   1   23157   23155
232445  0   0   23158   23156
232445  0   0   23159   23157
232445  0   0   23159   23157
232445  0   1   23159   23157
232445  0   0   23160   23158
112344  0   0   23135   23133
112344  0   0   23135   23133
112344  0   1   23135   23133
112344  0   1   23136   23134
112344  0   0   23136   23134
112344  0   1   23137   23135
112344  0   1   23137   23135
;
run;

data have;
set have;
obs= _n_;
run;

proc sql;
create table want as
select distinct t1.*, sum(t2.var1) as sum_var1, sum(t2.var2) as sum_var2
from have as t1
left join have as t2
on t1.id=t2.id and t2.dt between t1.sum_to_date and t1.dt-1
group by t1.obs
order by t1.obs;
quit;

@mfowlk wrote:

I'm trying find a sum for var1 and separate sum for var2 for each row based on the sas date column labelled dt and two days prior (which is in the sum_to_date) within each ID.

 

For example,

ID232445

line 5 sum(var1) = 0 sum(var2) =1

line 6 sum(var1) = 0 sum(var2) =2

line 7 sum(var1) = 0 sum(var2) =3

line 8 sum(var1) = 0 sum(var2) =3 

line 9 sum(var1) = 0 sum(var2) =4

line 10 sum(var1) = 0 sum(var2) =3

line 11 sum(var1) = 0 sum(var2) =1

line 12 sum(var1) =0 sum(var2)=2

continue summing until reaching ID 112344, then begin the summing again.Screenshot 2023-07-05 153753.png

 

 


 

mfowlk
Calcite | Level 5

Thank you. Yes, lines 1-4 should have answers as well, but I only included 1 month and the dates early in the month will not have prior dates to sum. For the ouput, I would like to include all the columns that are listed for each observation (ID, var1, var2, dt, sum_to_date) as well as the summation of var1 and var2.

 

Would the monotonic function help to uniquely identify each row?

 

Yes, the end of the interval would be include in the sum. So the observation/row would be possibly summing 3 numbers if there is a date that is within 2 days prior to the date on the observation/row.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 850 views
  • 4 likes
  • 3 in conversation