Data comprises of several columns including customer ID but need to pull data of only from the row with minimum date from (DATEVAL) for each customer, however this minimum date has to be within a range eg: within Dec'20 month. And finally sum the amount(AMT) of that customer for that month only.
Could you please provide a small set of your sample data, it is quite ambiguous to me regarding your request?
Does next code fits you requirements? If not - please explain what is the issue.
(coed was edited to fix bugs )
data have;
length city $12 month $7;
input ID DATE :date9. AMT CITY $ CODE $;
month = put(date, yymms7.);
cards;
1 07SEP2020 10000 LONDON PPS
1 11DEC2020 50000 BELARUS PPK
2 06DEC2020 40000 COLOMBO PPS
2 28JAN2021 30000 MUMBAI PPL
2 02DEC2020 20000 MUNICH PPS
; run;
/* sort and select 1st month of ID */
proc sort data=have; by ID date; run;
data to_select;
set have;
by ID;
if first.ID then output; /* or select by required month "yyyy/mm" */
keep ID month;
run;
/* select data to deal with and sum amount */
proc sql;
select a.ID,
sum(amt) as total
from have as a
right join to_select as b
on a.ID=b.ID and a.month = b.month
group by a.ID;
quit;
@udupa_13 wrote:
Data is already present in the server, so I need only the query to get the desired output without making a new dataset
The step creating the dataset "have" is only part of the code, because you failed to provide the data in usable form.
The requirement "need to pull data of only from the row with minimum date" seems to contradict "sum the amount(AMT) of that customer for that month only", so please post data in usable form and what you expect as result.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.