- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I'm sorry, but that doesn't seem to fix it, and I checked with edit mode that there are no extra blank characters
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.