Help using Base SAS procedures

Date Calculation?

Accepted Solution Solved
Reply
Super Contributor
Posts: 398
Accepted Solution

Date Calculation?

Hello:

 

I have a sample dataset shown below.   I would like to do the following:

 

1. Change the ProduceDate from Character to Date format as MMDDYYYY (such as 06/04/1963).

2. Create a new ReceiveDate which is 90 days from ProduceDate.

3. Create a new ShipDate which is 6 months from ProdeceDate.

 

Please advice how to do it, thanks. 

 

data test; 
      infile datalines dlm='|'; 
  input ID $10. ProduceDate : $10. State : $10.; 
datalines;                  
100012556 | 19980411 |  GA |   
600012956 | 19850321 |  IA |   
500012586 | 19630604 |  CA |  
500052586 | 19970530 |  TX | 
200052576 | 19990702 |  OH |   
600052577 | 19990806 |  TX |   
900052578 | 19990429 |  NY |  
800012578 | 19980527 |  NV |  
600012878 | 19990911 |  FL | 
300022578 | 19850821 |  PA |   
; 

Proc sort; by ProduceDate; run;

Accepted Solutions
Solution
‎05-01-2018 03:24 PM
PROC Star
Posts: 1,836

Re: Date Calculation?

something like this?

 

data test; 
      infile datalines dlm='|'; 
  input ID $10. ProduceDate : $10. State : $10.; 
datalines;                  
100012556 | 19980411 |  GA |   
600012956 | 19850321 |  IA |   
500012586 | 19630604 |  CA |  
500052586 | 19970530 |  TX | 
200052576 | 19990702 |  OH |   
600052577 | 19990806 |  TX |   
900052578 | 19990429 |  NY |  
800012578 | 19980527 |  NV |  
600012878 | 19990911 |  FL | 
300022578 | 19850821 |  PA |   
; 


data want;
set test(rename=ProduceDate=_ProduceDate);
ProduceDate=input(_ProduceDate,yymmdd10.);
ReceiveDate =intnx('days',ProduceDate,90);
ShipDate=intnx('month',ProduceDate,6);
format ProduceDate ReceiveDate ShipDate MMDDYY10.;
drop _:;
run;

View solution in original post


All Replies
Solution
‎05-01-2018 03:24 PM
PROC Star
Posts: 1,836

Re: Date Calculation?

something like this?

 

data test; 
      infile datalines dlm='|'; 
  input ID $10. ProduceDate : $10. State : $10.; 
datalines;                  
100012556 | 19980411 |  GA |   
600012956 | 19850321 |  IA |   
500012586 | 19630604 |  CA |  
500052586 | 19970530 |  TX | 
200052576 | 19990702 |  OH |   
600052577 | 19990806 |  TX |   
900052578 | 19990429 |  NY |  
800012578 | 19980527 |  NV |  
600012878 | 19990911 |  FL | 
300022578 | 19850821 |  PA |   
; 


data want;
set test(rename=ProduceDate=_ProduceDate);
ProduceDate=input(_ProduceDate,yymmdd10.);
ReceiveDate =intnx('days',ProduceDate,90);
ShipDate=intnx('month',ProduceDate,6);
format ProduceDate ReceiveDate ShipDate MMDDYY10.;
drop _:;
run;
Super User
Posts: 6,785

Re: Date Calculation?

Posted in reply to novinosrin

ProduceDate looks good.

 

ReceiveDate looks right, but could easily be simplified:

 

ReceiveDate = ProduceDate + 90;

 

ShipDate will revert to the first day of the month.  To get 6 months later, while using the same day of the month:

 

ShipDate = intnx('month', ProduceDate, 6, 'same');

Super Contributor
Posts: 398

Re: Date Calculation?

Posted in reply to Astounding

Hi Astounding,  your code is easy and quick.   Awesome!

Valued Guide
Posts: 597

Re: Date Calculation?

Read the ProduceDate as Date instead of Char. If you need same day 6 months from the ProduceDate then use "SameDay" in INTNX

data test; 
format ProduceDate ShipDate ReceiveDate yymmdd10.;
      infile datalines dlm='|'; 
  input ID $10. ProduceDate : yymmdd8. State : $10.;
 ReceiveDate =intnx('days',ProduceDate,90);
ShipDate=intnx('month',ProduceDate,6,"sameday");
datalines;                  
100012556 | 19980411 |  GA |   
600012956 | 19850321 |  IA |   
500012586 | 19630604 |  CA |  
500052586 | 19970530 |  TX | 
200052576 | 19990702 |  OH |   
600052577 | 19990806 |  TX |   
900052578 | 19990429 |  NY |  
800012578 | 19980527 |  NV |  
600012878 | 19990911 |  FL | 
300022578 | 19850821 |  PA |   
; 
Run;
Thanks,
Suryakiran
Frequent Contributor
Posts: 75

Re: Date Calculation?

Posted in reply to SuryaKiran

@Astounding  has already proposed "SameDay" in INTNX to @novinosrin solution. 

@ybz12003's 1st requirement is 1. Change the ProduceDate from Character to Date format as MMDDYYYY probably means his/her real data has a character date 

Super Contributor
Posts: 398

Re: Date Calculation?

Thank you so much for your wonderful help.   I combined Novinosrin and Astounding's codes to get it work.   Unfortunately,  I only could give one solution to one person.   Because Novinosin answered the Q first, I give the credit to him.   But I am appreciated all of your kind help.  Smiley Happy

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 218 views
  • 3 likes
  • 5 in conversation