SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Getting YEAR from datadate.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Getting YEAR from datadate.

I have datasets as follows:

 

GVKEY DATADATE

0111     19740131

0111     19740231

.    

.   

.  

.

.

0111    20150131

.

I want to  create new variables--year and month from the datadate. What would be the code both in data step and in proc sql?  

Thank you. 

 


Accepted Solutions
Solution
‎02-09-2017 10:20 PM
Trusted Advisor
Posts: 1,368

Re: Getting YEAR from datadate.

In case DATADATE is a character variable do:

 

year = input(substr(datadate,1,4),4.);

month = input(substr(datadate,5,2),2.);

 

In case the variable is numeric do:

year = int(datadate/10000);  

   or year = input(substr(put(datadate,8.),1,4),4.);

month = input(substr(put(datadate,8.),5,2),2.);

 

in case datadate is a date variable with predefined format of yymmdd8. then

    year = year(datadate);

   month = month(datadate);

 

View solution in original post


All Replies
Solution
‎02-09-2017 10:20 PM
Trusted Advisor
Posts: 1,368

Re: Getting YEAR from datadate.

In case DATADATE is a character variable do:

 

year = input(substr(datadate,1,4),4.);

month = input(substr(datadate,5,2),2.);

 

In case the variable is numeric do:

year = int(datadate/10000);  

   or year = input(substr(put(datadate,8.),1,4),4.);

month = input(substr(put(datadate,8.),5,2),2.);

 

in case datadate is a date variable with predefined format of yymmdd8. then

    year = year(datadate);

   month = month(datadate);

 

Occasional Contributor
Posts: 17

Re: Getting YEAR from datadate.

Yes, it works. Thank you.

Super User
Posts: 6,928

Re: Getting YEAR from datadate.

Please mark your question as solved (accept @Shmuel's post as solution)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 17

Re: Getting YEAR from datadate.

Thank you.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 433 views
  • 3 likes
  • 3 in conversation