BookmarkSubscribeRSS Feed
carl_miles
Fluorite | Level 6
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 

4 REPLIES 4
Amir
PROC Star

Hi,

 

How about something like:

 

data want;
  merge
    customerorders2
    customerorders2(keep     = OrderDate2
                    rename   = OrderDate2 = OrderDate2next
                    firstobs = 2)
  ;
run;

 

Which gives:

 

Amir_0-1695206585076.png

 

 

 

Thanks & kind regarads,

Amir.

mkeintz
PROC Star

"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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
carl_miles
Fluorite | Level 6
Thanks mkeintz Yes My table have many records
carl_miles
Fluorite | Level 6

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 735 views
  • 0 likes
  • 3 in conversation