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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.