Help using Base SAS procedures

Problem when formatting a date variable

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

Problem when formatting a date variable

Hey all,

I am trying to format a date variable that currently has a date16 format.

There are . for missing values for the variable. I need to reformat this but I get an ERROR: There was a problem with the format so BEST. was used.

Not sure what is going on. or how to work around.

Thanks


Accepted Solutions
Solution
‎08-13-2013 05:00 PM
Regular Contributor
Posts: 180

Re: Problem when formatting a date variable

Posted in reply to Martinez77

Try this to reformat "mydate":

mydate=datepart(mydate);
put mydate mmddyyn8.;

Regards,

View solution in original post


All Replies
Super User
Posts: 19,850

Re: Problem when formatting a date variable

Posted in reply to Martinez77

What does date16. look like? In my version of SAS date9. is the biggest format I can have.

Post what your dates currently look like and what you want them to look like, eg. reformat to.

Contributor
Posts: 27

Re: Problem when formatting a date variable

Reeza

29MAR2013:00:00:00

This is what they currently look like.

I want it to appear like this: 03292013

Thanks you for your help,

Martinez77

Solution
‎08-13-2013 05:00 PM
Regular Contributor
Posts: 180

Re: Problem when formatting a date variable

Posted in reply to Martinez77

Try this to reformat "mydate":

mydate=datepart(mydate);
put mydate mmddyyn8.;

Regards,

Contributor
Posts: 27

Re: Problem when formatting a date variable

I should have been a little more clear.

I have a variable in my data set name enrl_drop_dt which contains date values as below:

29MAR2013:00:00:00

There are missing values with a . if missing.

I need them to display as shown below:

03292013

I was hoping I could do it in a data step

Data bla;

set bla;

format enrl_drop_dt mmddyyy6.;

run;

I hope that shines some light

Thanks,

Martinez

Super User
Posts: 5,516

Re: Problem when formatting a date variable

Posted in reply to Martinez77

The format to use would be:

mmddyyn8.

There is some question as to whether that would apply as is, or whether you need to change your data as well:

enrl_drop_dt = datepart(enrl_drop_dt);

It all depends on what your variable contains.  If it's a date variable, leave it as is.  If it's a date-time variable, you'll have to apply DATEPART.  So try it with just the format and see how the dates look.  If they are far too large, apply DATEPART.

Contributor
Posts: 27

Re: Problem when formatting a date variable

Posted in reply to Astounding

all

Thank you all very much. I had a typo in my format. Fixed it and the datepart function worked great.

Another sas win.

Thanks,

Martinez

Super User
Posts: 19,850

Re: Problem when formatting a date variable

Posted in reply to Martinez77

So your variable is actually a datetime variable, not a date variable.

You can use some date formats specifically on datetime variables, but the one you require isn't...AFAIK. That leaves you with two options, 1) Recode your variable to a true date variable so you can apply the date variable or 2) Create your own format that you can apply. 

The first option was suggested by CTorres above in a data step.  The second partly depends on what version of SAS you have, do you have SAS 9.3. In SAS 9.3 you can use functions in a format so it becomes relatively straight forward then.

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 751 views
  • 6 likes
  • 4 in conversation