DATA Step, Macro, Functions and more

Date Conversion

Accepted Solution Solved
Reply
Contributor
Posts: 56
Accepted Solution

Date Conversion

I know there is a simple solution but I am having trouble finding it here..So if someone know where the solution is, can you please just post the link.

 

I am trying to change the date format from '01JAN2017' to '0116'.   The '01JAN2017' field is currently in numeric date9. format.

 

Thank you.


Accepted Solutions
Solution
‎09-29-2017 01:58 PM
Super User
Super User
Posts: 8,289

Re: Date Conversion

Posted in reply to tobyfarms

Why would you want the value in MMYY order? That will not sort properly and would be very confusing. Why YYMM?

Do you want a string?

str = substr(put(date,ddmmyy6.),3,4);

A number?

numb = month(date)+100 + year(date)-2000 ;
format numb z4. ;

If a number do want to store the actual date and just attach a format?

proc format;
 picture mydt other='%0m%0y' (datatype=date);
run;
....
format date mydt4.;

 

View solution in original post


All Replies
Respected Advisor
Posts: 3,293

Re: Date Conversion

Posted in reply to tobyfarms

Change the format on this variable to MMDDYY4.

--
Paige Miller
Super User
Posts: 13,950

Re: Date Conversion

Posted in reply to tobyfarms

tobyfarms wrote:

I know there is a simple solution but I am having trouble finding it here..So if someone know where the solution is, can you please just post the link.

 

I am trying to change the date format from '01JAN2017' to '0116'.   The '01JAN2017' field is currently in numeric date9. format.

 

Thank you.


Where does the 16 come from for 01Jan2017? If isn't a month number, a day number or any reasonable year value?

If you mean 0101 for 01Jan2017 then @PaigeMiller is correct.

Contributor
Posts: 56

Re: Date Conversion

@ballardw Sorry that was a mistake. I meant to have '0117', capturing the monthYear.  

Contributor
Posts: 30

Re: Date Conversion

Posted in reply to tobyfarms
data want;
format date mmyyn4.;
date='01jan2017'd;
run;

Try this

 

Trusted Advisor
Posts: 1,400

Re: Date Conversion

[ Edited ]
Posted in reply to tobyfarms

How about 2-digit year first, then 2-digit month?  If so, you can use the yymmdd format, truncated to 4 characters.

 

data _null_;

  d='27sep2017'd;

  put d=yymmdd4.;

run;

 

 

editted additon:  I didn't see a MMYY format, but @PBsas did.  So you can ignore the below:

 

If you really need MMYY, I don't see a MMYY format.  But you can make your own, using the PICTURE statement and the DATATYPE option, as in:

 

 

proc format;
  picture myformat (default=4)
    low-high = '%0m%0y' (datatype=date);
run;

data _null_;
  d=today();
  put d=myformat. ;
run ;

 

Solution
‎09-29-2017 01:58 PM
Super User
Super User
Posts: 8,289

Re: Date Conversion

Posted in reply to tobyfarms

Why would you want the value in MMYY order? That will not sort properly and would be very confusing. Why YYMM?

Do you want a string?

str = substr(put(date,ddmmyy6.),3,4);

A number?

numb = month(date)+100 + year(date)-2000 ;
format numb z4. ;

If a number do want to store the actual date and just attach a format?

proc format;
 picture mydt other='%0m%0y' (datatype=date);
run;
....
format date mydt4.;

 

Contributor
Posts: 56

Re: Date Conversion

The Substr function worked for what I am trying to achieve in my coding. This populate the 2-digit month and 2 digit year from my current date format. Thanks @Tom
Frequent Contributor
Posts: 118

Re: Date Conversion

Posted in reply to tobyfarms

Try this...

 

data _null_;
  today=today();
format today MMYYN4.;
PUT TODAY;
run;

Hope this helps.

☑ This topic is solved.

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

Discussion stats
  • 8 replies
  • 234 views
  • 1 like
  • 7 in conversation