DATA Step, Macro, Functions and more

Month/Year to Date Conversion and Display

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 8
Accepted Solution

Month/Year to Date Conversion and Display

[ Edited ]

Hi there,

 

I am trying to convert character formatted date which look like OCT2017_Default to date formatted OCT2017.

 

Thanks in advance.


Accepted Solutions
Solution
Wednesday
Super User
Posts: 9,880

Re: Month/Year to Date Conversion and Display

[ Edited ]
Posted in reply to kamlesh_suthar

Use the monyy informat/format:

data _null_;
date_str = "OCT2017_Default";
date_num = input(date_str,monyy7.);
format date_num monyy7.;
put date_num=;
run;

And use a more descriptive subject next time. "Base SAS" in the "Base SAS Programming" community is not showing off a lot of creativity, to be very polite.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
Wednesday
Super User
Posts: 9,880

Re: Month/Year to Date Conversion and Display

[ Edited ]
Posted in reply to kamlesh_suthar

Use the monyy informat/format:

data _null_;
date_str = "OCT2017_Default";
date_num = input(date_str,monyy7.);
format date_num monyy7.;
put date_num=;
run;

And use a more descriptive subject next time. "Base SAS" in the "Base SAS Programming" community is not showing off a lot of creativity, to be very polite.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 2,812

Re: Month/Year to Date Conversion and Display

Posted in reply to KurtBremser

I did not know that the MONYY7. format would exclude the part of the text string that is '_Default', I thought this would cause the conversion to fail.

 

Learn something new every day!

--
Paige Miller
Super User
Posts: 9,880

Re: Month/Year to Date Conversion and Display

Posted in reply to PaigeMiller

This seems to be a feature of numeric informats, they only use the number of characters specified:

data _null_;
num_str = "1234567some_garbage";
num_num = input(num_str,7.);
put num_num=;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 2,812

Re: Month/Year to Date Conversion and Display

Posted in reply to KurtBremser

So this fails, as I would have expected

 

num_num = input(num_str,8.);
--
Paige Miller
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 92 views
  • 2 likes
  • 3 in conversation