SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Are Date9. and Time5. formats always numeric?

Reply
New Contributor
Posts: 3

Are Date9. and Time5. formats always numeric?

I have tried various ways of converting Date9. and Time5. variables to character format but cant achieve it including PUT, INPUT, INFORMAT and FORMAT descriptions.

Are Date9. and Time5. fields always numeric?

Super User
Posts: 9,572

Re: Are Date9. and Time5. formats always numeric?

Numeric formats are numeric formats, they display (or convert in the case of the put() function) numeric data as strings.

Numeric informats are used to read (input statement) or convert (input() function) character data to numeric variables/values.

 

Any variable that has a numeric format attached to it must be numeric by definition.

 

Since a variable cannot change its type, you can't do (eg)

datevar = put(datevar,date9.);

You have to create a new variable of type character for this.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
New Contributor
Posts: 3

Re: Are Date9. and Time5. formats always numeric?

Posted in reply to KurtBremser

Thanks for replying. Using the below, the DATE9. element changes to $9 - it is not possible to keep DATE9. when changing to character?

 

informat PCDAC1 date9.;

format PCDAC1 date9.;

PCDAC=put(PCDAC1, $date9.);

run;

Super User
Posts: 9,572

Re: Are Date9. and Time5. formats always numeric?


Kirsty wrote:

Thanks for replying. Using the below, the DATE9. element changes to $9 - it is not possible to keep DATE9. when changing to character?

 

informat PCDAC1 date9.;

format PCDAC1 date9.;

PCDAC=put(PCDAC1, $date9.);

run;


You are PUTting a numeric variable, so you need a numeric format:

PCDAC=put(PCDAC1, date9.);

The new variable does not need a format, as its contents are already formatted as specified.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Highlighted
Super User
Posts: 13,029

Re: Are Date9. and Time5. formats always numeric?


Kirsty wrote:

Thanks for replying. Using the below, the DATE9. element changes to $9 - it is not possible to keep DATE9. when changing to character?

 

informat PCDAC1 date9.;

format PCDAC1 date9.;

PCDAC=put(PCDAC1, $date9.);

run;


SAS Character variables unless you assign one of the very limited number of character formats or create a custom format will pretty much have $XX. with xx indicating the maximum length of the variable to display.

If you ever want to use any of the functions that extract values from dates or times such as Year, Month, Day, Hour or Minute functions, or use the interval functions such as Intnx or Intck then you do not want a character valued date.

Super User
Posts: 22,845

Re: Are Date9. and Time5. formats always numeric?

Yes, SAS stores dates, times and datetimes as numbers, in number of days, and seconds respectively. 

 

To convert to a character, you can use PUT with the format you want it to appear with. 

 

NEW_VAR = PUT  ( OLD_VAR, <FORMAT TO APPEAR>) ;

However, with a character variable you dates will never sort properly and you can't calculate any type of intervals or durations or change how it's aggregated. In general, leaving it as a numeric with the proper format is better.

 

data test;
format date date9.;
input date;
cards;
20001
20002
20004
;
run;

proc contents data=test;run;

proc print data=test;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 379 views
  • 5 likes
  • 4 in conversation