- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;