DATA Step, Macro, Functions and more

Finding Month and day from number of days away from January 1.

Accepted Solution Solved
Reply
Occasional Contributor pr1
Occasional Contributor
Posts: 15
Accepted Solution

Finding Month and day from number of days away from January 1.

Hello, I have a number that represents the number of days from January 1 of any year with January 1 as day 1. For example 10 will be January 10. 

Using SAS how can I convert that number into day and month for example 10 = 01/10?

 

Thanks.

 

Pramodini


Accepted Solutions
Solution
‎05-25-2016 10:32 AM
Super User
Posts: 5,513

Re: Finding Month and day from number of days away from January 1.

[ Edited ]

OK, it's easy enough to split this in two.  These statements assume that your original variable is numeric.  If it is actually character, the statements would change slightly.

 

year = int(juldateval / 1000);

days = mod(juldateval, 1000);

newdate = mdy(1, 1, year) + days - 1;

format newdate mmddyys10.;

 

 

View solution in original post


All Replies
Super User
Posts: 5,513

Re: Finding Month and day from number of days away from January 1.

How do you intend to supply the year?  Getting the correct answer depends on whether or not it is a leap year.

Occasional Contributor pr1
Occasional Contributor
Posts: 15

Re: Finding Month and day from number of days away from January 1.

Posted in reply to Astounding

I have  1956229 as input field that should translate into 8/16/1956

Super User
Super User
Posts: 7,970

Re: Finding Month and day from number of days away from January 1.

Where is the data from?  I mean I can see year=1956 as first four characters, but the rest doesn't make sense and formating as date value is wrong.

Occasional Contributor pr1
Occasional Contributor
Posts: 15

Re: Finding Month and day from number of days away from January 1.

1956229 as a field in which the year is 1956 and 229 is the 229th day of year 1956 that is August 16th which should display as 8/16/1956

Solution
‎05-25-2016 10:32 AM
Super User
Posts: 5,513

Re: Finding Month and day from number of days away from January 1.

[ Edited ]

OK, it's easy enough to split this in two.  These statements assume that your original variable is numeric.  If it is actually character, the statements would change slightly.

 

year = int(juldateval / 1000);

days = mod(juldateval, 1000);

newdate = mdy(1, 1, year) + days - 1;

format newdate mmddyys10.;

 

 

Occasional Contributor pr1
Occasional Contributor
Posts: 15

Re: Finding Month and day from number of days away from January 1.

Posted in reply to Astounding

THANKS!!  that works. 

Super User
Posts: 10,035

Re: Finding Month and day from number of days away from January 1.

There already is a funtion to do that .

 

data _null_;
Xend=datejul(2001001);
put Xend / Xend date9.; 
run;
Occasional Contributor pr1
Occasional Contributor
Posts: 15

Re: Finding Month and day from number of days away from January 1.

Thank You.  Let me try this.

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 464 views
  • 3 likes
  • 4 in conversation