DATA Step, Macro, Functions and more

Need to help to create new variables from date_variables "01Jan2005"

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Need to help to create new variables from date_variables "01Jan2005"

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.


Accepted Solutions
Solution
‎04-05-2016 10:35 PM
Respected Advisor
Posts: 4,646

Re: Need to help to create new variables from date_variables "01Jan2005"

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


All Replies
Contributor
Posts: 37

Re: Need to help to create new variables from date_variables "01Jan2005"

Please I need the help!Anyone knows?
Respected Advisor
Posts: 3,124

Re: Need to help to create new variables from date_variables "01Jan2005"

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.

 

Super User
Posts: 5,082

Re: Need to help to create new variables from date_variables "01Jan2005"

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.

Solution
‎04-05-2016 10:35 PM
Respected Advisor
Posts: 4,646

Re: Need to help to create new variables from date_variables "01Jan2005"

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
Contributor
Posts: 37

Re: Need to help to create new variables from date_variables "01Jan2005"

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.
Contributor
Posts: 37

Re: Need to help to create new variables from date_variables "01Jan2005"

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.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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