BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mauri0623
Quartz | Level 8

I have a character date that is like 10/8/2029 that I would like to convert it  to 20190810. I have applied many thing

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

If you run PROC CONTENTS on this data set, does it say the variable that contains 10/8/2029 is numeric or character?

 

Is there a typo in the year?

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

If you run PROC CONTENTS on this data set, does it say the variable that contains 10/8/2029 is numeric or character?

 

Is there a typo in the year?

--
Paige Miller
mauri0623
Quartz | Level 8

10/8/2019 is a character of length  8.

ballardw
Super User

You did not tell us the name of the variable but this is an example of code that would create a SAS date valued numeric and apply a format for the appearance you want.

 

data want;
   set have;
   newdate = input(characterdate,ddmmyy10.);
   format newdate yymmddn8.;
run;

Once you have the value in the Newdate variable you can change appearance as needed by using a different format. If SAS doesn't provide a date appearance you need then likely you can create one using Proc Format.

PaigeMiller
Diamond | Level 26

So first lets straighten out the terminology.

 

When you say "I have a character date", this can be confusing — a SAS date can only be numeric, and it must be the number of days since 01JAN1960 (although a SAS date can be formatted, for example, to appear as 01-01-1960). So you do not have a character DATE, you have a character STRING (or character variable) whose value is 10/8/2019.

 

Also, 10/8/2019 is 9 characters so it must have a length of 9.

 

Now, to convert this character string to appear differently, such as 20190810, the best and easiest way to do this is to use the built in SAS date functions and SAS date informats and SAS date formats. Why? Because SAS has already done the hard work of programming all these conversions, so you don't have to.

 

So here I use the informat ANYDTDTE to turn your character string into a SAS date value (numeric), and I use the format YYMMDDN8. make the date appear as 20191008.

 

data have;
    y='10/8/2019';
run;
data want;
    set have;
    y1=input(y,anydtdte.);
    format y1 yymmddn8.;
run;

NOTE: your original date 10/8/2019 has the 8 in the second position, I assume it is day 8 of month 10, and so this output produces a value of Y1 that has day 8 of month 10, despite the fact that you specifically asked for 20190810 where the 8 and 10 have been reversed. When you provide examples of dates, and the month is <=12 and the day is <=12, we don't know which is the month and which is the day, and so your example data could be improved to make this clear.

 

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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