BookmarkSubscribeRSS Feed
Kirsty
Calcite | Level 5

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?

5 REPLIES 5
Kurt_Bremser
Super User

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.

Kirsty
Calcite | Level 5

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;

Kurt_Bremser
Super User

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

ballardw
Super User

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

Reeza
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 3121 views
  • 5 likes
  • 4 in conversation