I would like to know how I can create three variables date, month and year from date_variable (example: 01Jan2005). I have a column of date_variable,but I want to extract it into date_variable, month_variable, and year_variable. Please suggest me. Thanks.
data test;
date_str = "01Jan2005";
date_var = "01Jan2005"d;
/* Or */
date_var = input("01Jan2005", date9.);
format date_var yymmdd10.;
day_var = day(date_var);
month_var = month(date_var);
year_var = year(date_var);
run;
proc print; run;
Ok. see example below:
data test;
date='01jan2005'd;
day=day(date);
month=month(date);
year=year(date);
run;
However, my question for you is: what is the big picture problem you are facing?There maybe better solutions from the root or beginning.
When you say your date variable might be 01JAN2005 ... it makes a big difference whether or not this is stored as a character string. It also makes a big difference as to what you want the output to be. So, assuming your original variable is a character string, and assuming you want three numeric variables as the result, this would work:
day = input(date_var, 2.);
month = month(input(date_var, date9.));
year = year(input(date_var, date9.));
If what you want is something different, you'll have to provide more details.
data test;
date_str = "01Jan2005";
date_var = "01Jan2005"d;
/* Or */
date_var = input("01Jan2005", date9.);
format date_var yymmdd10.;
day_var = day(date_var);
month_var = month(date_var);
year_var = year(date_var);
run;
proc print; run;
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.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.