SAS Year Variable to a general date (ex: 20180101)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

SAS Year Variable to a general date (ex: 20180101)

Good day SAS Community,

 

I have a question concerning manipulation of dates in SAS. Suppose I have a numerical variable for year (ex: 2016, 2017, etc...) and I instead wanted to generate a second date (DATE9.) variable that would be set to January 1st, for each of those years (ex: 2016-01-01, 2017-01-01).

 

How would I go about doing this in SAS?

 

idyearnew_date
1201501JAN2015
2201401JAN2014
3200801JAN2008
4199901JAN1999

 

Thank you again for your help,

-Carmine


Accepted Solutions
Solution
a week ago
Super User
Posts: 2,049

Re: SAS Year Variable to a general date (ex: 20180101)

Posted in reply to Carmine_Rossi

if year is numeric years

 

new_date=mdy(1,1,year);

format new_date date9.;

 


data w;
do year=2015 to 2025;
new_date=mdy(1,1,year);
output;
end;
format new_date date9.;
run;

View solution in original post


All Replies
Contributor
Posts: 42

Re: SAS Year Variable to a general date (ex: 20180101)

Posted in reply to Carmine_Rossi

@Carmine_Rossi
      You may try this : 

 

data want;
          set have; 
         dt = DatePart(new_date); 
         Year1 = Year(dt); 
          Month1 = Month(dt);
          Day1 = Day(dt); 
          Hour1 = Hour(dt); 
run;
Solution
a week ago
Super User
Posts: 2,049

Re: SAS Year Variable to a general date (ex: 20180101)

Posted in reply to Carmine_Rossi

if year is numeric years

 

new_date=mdy(1,1,year);

format new_date date9.;

 


data w;
do year=2015 to 2025;
new_date=mdy(1,1,year);
output;
end;
format new_date date9.;
run;

Occasional Contributor
Posts: 10

Re: SAS Year Variable to a general date (ex: 20180101)

Posted in reply to novinosrin

Thank you very much! The MDY function is what I was looking for!

-Carmine

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 86 views
  • 0 likes
  • 3 in conversation