I have date in date9. format eg 2011-12-15
how can I convert it to 20111215
just take substring and concatenate or there is another specific sas date format for this??
The value 2011-12-15 is not date9. format.
Is the value an actual SAS date, ie. an integer or a character variable?
sorry I mean 15DEC2011
and convert to 20111215
If the value is an actual SAS date value (ie. an integer), then simply apply the appropriate format.
In this case, the yymmddn8. Format
data test;
dt = '15DEC2011'd;
format dt yymmddn8.;
run;
@HeatherNewton wrote:
I have date in date9. format eg 2011-12-15
how can I convert it to 20111215
just take substring and concatenate or there is another specific sas date format for this??
Dates are not IN a format. Dates are just the number of days since 1960.
You can display a date value USING a format.
To display a date value in the style yyyymmdd use the YYMMDDN8. format. It does not matter what format is currently ATTACHED to the variable.
So you can either use a FORMAT statement to attach the YYMMDDN8. format to the variable.
Or use the YYMMDDN8. format with the variable in the PUT statement to write an 8 digit string.
Or use the YYMMDDN8. format with the variable in a PUT() function call to generate an 8 digit string.
FORMATS convert values to text. INFORMATS convert text to values.
You use FORMATS with PUT statement, FORMAT statement, PUT(), PUTC() and PUTN() functions.
You use INFORMATS with INPUT statement, INFORMAT statement, iNPUT(), INPUTC() and INPUTN() functions.
Normally you don't need special informats (or formats). SAS already knows how to read and write strings and numbers. Usually it is only for DATE, TIME, and DATETIME values that you need. Although if you are reading a report you might need to use the COMMA informat to read text that has commas or dollar signs.
The INPUT statement is very complex. The simplest way to use is it what the documentation calls LIST MODE. In that mode the input just reads the next "word" or "field" from the text. If you need to read something like a DATE that needs a special informat to convert the written text into a value you could just attach the informat to the variable and then you don't have to mention it in the INPUT statement itself. Since each variable reads the next word you need to use a period in the text to indicate where an empty or missing values is needed. The period will work for both numeric and character values as the normal informat for character values ($ informat) will convert a single period into a blank value.
With FORMATTED MODE input you follow each variable name with the informat you want it to read. In that mode the INPUT statement does not look for "words" or "fields". It just reads the number of bytes you asked it to read. This works great when the text being read has the values in fixed positions on the line. It also means that an empty or missing value can just be represented by spaces.
If you want to read the values in LIST MODE and one of the variables requires a special informat you don't have to attach an INFORMAT to the variable. Instead you can use a colon as a modifier before the informat in the INPUT statement. That tells the INPUT statement to read the next "word" or "field" in list mode, but to use the specified informat instead of the default informat for that variable. This is also helpful for character variables as SAS will use the width of the informat specification used to GUESS what length to define the variable when you have not already defined with with a LENGTH statement.
input id :$10. apple orange cost date :yymmdd. ;
format date yymmdd10.;
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!
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.
Ready to level-up your skills? Choose your own adventure.