I am trying to sum units based on the start_dt where concent_cnt is 1 to 5
i need a sum of units based on range of dates
Basically you need GROUP BY, not ORDER BY:
proc sql;
select np_resource_id,start_dt,activity_dt as end_dt,
sum(unit) as total_units,
max(consec_cnt) as final_consec,
end_dt-start_dt as difference
from have
group by np_resource_id,start_dt
having consec_cnt=final_consec and final_consec >=5;
quit;
This will produce one record per np_resource_id/start_dt combination, but just those with a maximum consec_cnt>=5.
The program assumes for there are no "holes" in consec_cnt. I.e. if there is a consec_cnt=6, there must also be a 1,2,3,4, and 5.
Your dates look like they are character variables. If so you likely want to create new variable that are SAS date valued numeric to use "range" of dates.
What specific range of dates do you need? You don't mention it. Also you picture of the data does not include a variable named concent_cnt so saying anything about that variable isn't much help.
its actually consec_cnt (which is counting 5 consecutive days)
i do have a range of five days but when i sum the units it sums all the units but i want sum of units for specific dates
PROC SQL ;
CREATE TABLE NEW AS
SELECT NP_RESOURCE_ID, category, START_DT, ACTDATE As End_DT, CONSEC_CNT,
INTCK('day',START_DT, ACTDATE) as DAY_DIFFERENCE
FROM cosick1
WHERE CONSEC_CNT >= 5
ORDER BY NP_RESOURCE_ID, ACTDATE ;
QUIT
Basically you need GROUP BY, not ORDER BY:
proc sql;
select np_resource_id,start_dt,activity_dt as end_dt,
sum(unit) as total_units,
max(consec_cnt) as final_consec,
end_dt-start_dt as difference
from have
group by np_resource_id,start_dt
having consec_cnt=final_consec and final_consec >=5;
quit;
This will produce one record per np_resource_id/start_dt combination, but just those with a maximum consec_cnt>=5.
The program assumes for there are no "holes" in consec_cnt. I.e. if there is a consec_cnt=6, there must also be a 1,2,3,4, and 5.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.