data CustomerOrders2; input CustomerID2 OrderDate2 :date9.; format OrderDate2 date9.; datalines; 1001 01JAN2023 1001 15FEB2023 1001 12DEC2023 1001 25JAN2024 ; run;
So in above code we can see that Orderdate2
I want one more column that will be next date following will come as below mentioned output i want
CustomerID2 OrderDate2 Orderdateprev
1001 01JAN2023 15FEB2023
1001 15FEB2023 12DEC2023
1001 12DEC2023 25JAN2024
1001 25JAN2024 -
So the next immediate will append and if there is none then it will be null but it will still in cohort
Could someone guide me here
Hi,
How about something like:
data want;
merge
customerorders2
customerorders2(keep = OrderDate2
rename = OrderDate2 = OrderDate2next
firstobs = 2)
;
run;
Which gives:
Thanks & kind regarads,
Amir.
"Previous" orderdate? I hope you actually mean "next" orderdate, since "previous" suggests looking back, yet your example looks forward.
Also, I suspect your actual data has multiple ID's, and that you don't want the first orderdate of a new ID to contaminate the last record of the preceding ID. That assumption is in the code below:
data CustomerOrders2;
input CustomerID2 OrderDate2 :date9.;
format OrderDate2 date9.;
datalines;
1001 01JAN2023
1001 15FEB2023
1001 12DEC2023
1001 25JAN2024
run;
data want (drop=nxt_:);
merge customerorders2
customerorders2 (firstobs=2 keep=customerid2 orderdate2
rename=(customerid2=nxt_id orderdate2=orderdate_next));
if nxt_id^=customerid2 then orderdate_next=.;
run;
data CustomerOrders2;
input CustomerID2 earlier_date OrderDate2 :date9.;
format earlier_date OrderDate2 date9.;
datalines;
1001 15dec2022 01JAN2023
1002 15dec2022 15FEB2023
1003 15dec2022 12DEC2023
1004 15dec2022 25JAN2024
;
run;
i have the dataset like this
CustomerID2 Earlier_date OrderDate2
1001 15dec2022 01JAN2023
1001 15dec2022 15FEB2023
1001 15dec2022 12DEC2023
1001 15dec2022 25JAN2024
i want output dataset to be
CustomerID2 Earlier_date OrderDate2 Orderdateprev
1001 15dec2022 01JAN2023 15dec2022
1001 15dec2022 15FEB2023 01JAN2023
1001 15dec2022 12DEC2023 15FEB2023
1001 15dec2022 25JAN2024 12DEC2023
So i want output like above like the first orderdates orderdateprev will be earlier date
the second order date's orderdateprev will be the first orderdate2 like that my flow will be
Could someone guide me here
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.