BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

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??

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

The value 2011-12-15 is not date9. format.

 

Is the value an actual SAS date, ie. an integer or a character variable?

HeatherNewton
Quartz | Level 8

sorry I mean 15DEC2011 

and convert to 20111215

PeterClemmensen
Tourmaline | Level 20

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;
Tom
Super User Tom
Super User

@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.

HeatherNewton
Quartz | Level 8
So format just for displaying in the output dataset, right?

And put is also for displaying..

Another two i dont get is
Informat and input
When do i use which

If i have a datastep with datalines, mormally i use input
Eg input apple orange cost;
But i dont get how to use the infirmat after the column name so normally i leave it empty or minimal..
Another
Tom
Super User Tom
Super User

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.;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 427 views
  • 0 likes
  • 3 in conversation