The SAS Output Delivery System and reporting techniques

Convert a string year into a date year like wise 3 letter string month to date formatted month

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

Convert a string year into a date year like wise 3 letter string month to date formatted month

[ Edited ]

Hi,

I was trying to convert a year '2015' which is in character format into date format by using input function.
Eg : input(year, 4.) It either gives me a missing value or a value as 1965. Can some one help me what source informat should I be using

Like wise I have a column which has 3 letter month 'Feb' in character format and I used same input function and I get missing values. I tried with all possible source informats but still unable to find the right solution.

Please tell me if we can do it without concatenating year and month.

I want it individual conversion.

Thanks and Regards,
Karthik


Accepted Solutions
Solution
‎04-26-2016 04:42 AM
Grand Advisor
Posts: 16,306

Re: Convert a string year into a date year like wise 3 letter string month to date formatted month

 

I couldn't find informats so used some date logic to get it.  A SAS date requires a day, month and year though they don't need to be displayed. A date is the number of days since Jan 1, 1960.  

 

data have;
input year_char $ month_char $;
cards;
1990 SEP
2014 JAN
2015 MAY
;
run;

data want;
set have;

year=mdy(1, 1, input(year_char, 4.));
month=input(catt("01", month_char, "2015"), date9.);

format month monname3.;
format year year4.;
run;

proc print;
run;

 

View solution in original post


All Replies
Respected Advisor
Posts: 4,606

Re: Convert a string year into a date year like wise 3 letter string month to date formatted month

What messages do you get in the log window?

PG
Solution
‎04-26-2016 04:42 AM
Grand Advisor
Posts: 16,306

Re: Convert a string year into a date year like wise 3 letter string month to date formatted month

 

I couldn't find informats so used some date logic to get it.  A SAS date requires a day, month and year though they don't need to be displayed. A date is the number of days since Jan 1, 1960.  

 

data have;
input year_char $ month_char $;
cards;
1990 SEP
2014 JAN
2015 MAY
;
run;

data want;
set have;

year=mdy(1, 1, input(year_char, 4.));
month=input(catt("01", month_char, "2015"), date9.);

format month monname3.;
format year year4.;
run;

proc print;
run;

 

Esteemed Advisor
Esteemed Advisor
Posts: 6,690

Re: Convert a string year into a date year like wise 3 letter string month to date formatted month

To get a good answer, it is a good idea to post test data (in the form of a datastep) and what the output should look like.  I guess with the below:

data want;
  year="2015";
  month="Feb";
  numeric_year=input(year,4.);
  numeric_month=month(input(cats("01",month,"2000"),date9.));
run;
Post a Question
Discussion Stats
  • 3 replies
  • 401 views
  • 2 likes
  • 4 in conversation