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.
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);
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);
Yes, it works. Thank you.
Please mark your question as solved (accept @Shmuel's post as solution)
Thank you.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.