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

Hello

 

I have a text variable with the format "YYYY-MM-DD" that I would like to format as date, but as far as I can see SAS does not support this particular format. If it did, I guess it would be called "YYYYMMDD10.".

 

I guess I could use substr to grab year, month and date seperately, but I was hoping for a smarter solution.

 

I hope someone can help.

 

Kind regards,

 

Jacob

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You probably have some "hidden" characters in those files.  Could be just leading spaces that would cause the first 10 characters to not look like a valid date.  You could try using STRIP() function to remove leading/trailing spaces.

Otherwise print the values using $HEX function and figure out what is in there.  Could be things like carriage return, 0D, line feed, 0A, tab, 09, non-breaking space, A0, null, 00.  It could also be that the hyphens are not actually hyphens, but en or em dash characters.

View solution in original post

12 REPLIES 12
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, your post makes absolutely no sense.  Your subject is unkown timeformat, but then you proceed to talk about a date.  You provide the answer to your question and then say it doesn't exist?

data want;
  a="2018-10-10";
  b=input(a,yymmdd10.);
  format b date9.;
run;
jbrau123
Obsidian | Level 7

Dear RW9

 

I am a rookie so please apologize if my description is inadequate. I'm more used to other software, such as STATA.

 

I got an unknown format error when I tried to use the format logic of SAS although YYYYMM10. was not listed as known format in the SAS documentation I could find. You write "YY" not "YYYY". I assumed those two were different.

 

Your code seems to just make a new dataset with just one observation and two variables.

 

Kind regards,

 

Jacob

RW9
Diamond | Level 26 RW9
Diamond | Level 26

No probs.  A time format is different to a date format, the format being number of seconds since 00:00 displayed as a time, and the date being a count of days since the cuttoff point, displayed as a date, so different.  

Yes, there is no YYYYMMDD10 format, only YYMMDD10 format, the actual display is based on the length given in the format, so:
yymmdd8. will just display year as two numbers, where yymmdd10 will display year as four digits, per the documentation:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000197961.htm

 

The datastep was just to show you a working example of reading in per a format.

Kurt_Bremser
Super User

Maxim 1: read the documentation.

Browse documentation.sas.com

Select SAS 9.4 and Viya 3.4 Programming Documentation

There, navigate to

  Data Step Programming

    Formats and Informats

      SAS Informats

        Dictionary of Informats

          Informats by Category

Scroll down to category Date and Time

Browse through this list until you find what you need, which is the yymmdd informat in your case.

jbrau123
Obsidian | Level 7

Dear Kurt

 

Thank you very much,

 

documentation.sas.com seems like a very important reference to know. I will keep that for future reference!

 

Kind regards,

 

Jacob

Kurt_Bremser
Super User

@jbrau123 wrote:

Dear Kurt

 

Thank you very much,

 

documentation.sas.com seems like a very important reference to know. I will keep that for future reference!

 

Kind regards,

 

Jacob


It will take some time to get used to the navigational layout (especially for those of us who are used to the previous online doc at support.sas.com), and it's not complete by far yet, but I've seen the development over the past few months, and it's promising.

 

Astounding
PROC Star

SAS won't format text strings as dates.  Dates in SAS have to be converted to a number, and a format applied to the number.  That's why the @RW9 solution applies the INPUT function.  If you want to replace the original text variable name with a numeric value, you need to jump through a hoop or two.  Since you didn't tell us the name of your variable, I'll call it DATEVAR:

 

data want;

set have;

tempvar = input(datevar, yymmdd10.);

format tempvar   /* FORMAT NAME GOES HERE, see below */;

drop datevar;

rename tempvar = datevar;

run;

 

The FORMAT statement can supply any date format that you would like.  A few examples:

 

mmddyy10.

yymmdd10.

yymmddd10.

yymmdds10.

date9.

 

 

jbrau123
Obsidian | Level 7

Thank you very much

 

I tried this:

 

data Samlet6;

set Samlet5;

tempvar=input(ansattid,yymmdd10.);

format tempvar date9.;

drop datevar;

rename tempvar=ansattid2; /* I preferred not to overwrite */

run;

 

but for some reason it only gave me the date for some, not all, of the observations (see picture below). I can't find any sensible explanation to this phenomenon. It is the exact same length and the same symbols, and the numbers are meaningful observations as dates. Therefore, I have no idea why the observations are being treated differently.

 

 

Unavngivet.png

 

Astounding
PROC Star

That's because your incoming data contains an extra charater in the middle.  Assuming the extra character is a blank, try it this way:

 

data Samlet6;

set Samlet5;

ansattid = compress(ansattid);

ansattid2 = input(ansattid, yymmdd10.);

format ansattid2 date9.;

run;

 

If that doesn't work because the extra character is a nonblank, use a more complex expression:

 

ansattid = compress(ansattid, '0123456789-', 'k');

 

The "k" instructs COMPRESS to keep (rather than remove) the designated characters.

jbrau123
Obsidian | Level 7

I'm sorry, but that doesn't seem to fix it, and I checked with edit mode that there are no extra blank characters

Tom
Super User Tom
Super User

You probably have some "hidden" characters in those files.  Could be just leading spaces that would cause the first 10 characters to not look like a valid date.  You could try using STRIP() function to remove leading/trailing spaces.

Otherwise print the values using $HEX function and figure out what is in there.  Could be things like carriage return, 0D, line feed, 0A, tab, 09, non-breaking space, A0, null, 00.  It could also be that the hyphens are not actually hyphens, but en or em dash characters.

Tom
Super User Tom
Super User

@jbrau123 wrote:

Hello

 

I have a text variable with the format "YYYY-MM-DD" that I would like to format as date, but as far as I can see SAS does not support this particular format. If it did, I guess it would be called "YYYYMMDD10.".

 

I guess I could use substr to grab year, month and date seperately, but I was hoping for a smarter solution.

 

I hope someone can help.

 

Kind regards,

 

Jacob


YYMMDD is the name of the informat you can use to convert strings that have dates specified in year, month, day order into actual date values. There is also a YYMMDD format (and many other date formats) to control how SAS displays the date value.

 

SAS also has an informat named ANYDTDTE that is useful for converting strings into dates. It can handle many different ways that your date value might appear in your character variable.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 2433 views
  • 0 likes
  • 5 in conversation