## Date Calculation?

Solved
Super Contributor
Posts: 398

# 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.

``````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.);
ShipDate=intnx('month',ProduceDate,6);
drop _:;
run;``````

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.);
ShipDate=intnx('month',ProduceDate,6);
drop _:;
run;``````
Super User
Posts: 6,785

## Re: Date Calculation?

ProduceDate looks good.

ReceiveDate looks right, but could easily be simplified:

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?

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;
infile datalines dlm='|';
input ID \$10. ProduceDate : yymmdd8. State : \$10.;
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?

@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.

☑ This topic is solved.