BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nzrdufiu
Obsidian | Level 7

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

4 REPLIES 4
Shmuel
Garnet | Level 18

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);

 

nzrdufiu
Obsidian | Level 7

Yes, it works. Thank you.

nzrdufiu
Obsidian | Level 7

Thank you.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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