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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21
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;
PG

View solution in original post

6 REPLIES 6
Cathy
Obsidian | Level 7
Please I need the help!Anyone knows?
Haikuo
Onyx | Level 15

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.

 

Astounding
PROC Star

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.

PGStats
Opal | Level 21
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;
PG
Cathy
Obsidian | Level 7
Thanks a lot, it is the numeric variable. And I have a column of date_variable;
cards;
gender date_variabe
F 01Jan2005
M 04Sep2007
M 09Oct2008
;
I want to create new 3 variables from date_variables, and I did as you suggested

day = input(date_var, 2.);
month = month(input(date_var, date9.));
year = year(input(date_var, date9.));

But all the columns are blank. Thanks a lot again. I will try with the last comment that you mentioned.
Cathy
Obsidian | Level 7
Thanks a lot Haikuo, Astounding, and PGStats. I got it, finally. It took me 3 or 4 days to understand. I am so happy now. Thanks again.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1071 views
  • 3 likes
  • 4 in conversation