## How to read data and time as a numeric variable?

Solved
Regular Contributor
Posts: 166

# How to read data and time as a numeric variable?

Hello everybody,

I have numeric variables which are showed below:

SAS Output:

 Checking numeric variables in the patients data set The MEANS Procedure Variable Label N N Miss Minimum Maximum Price Price 21174589 0 0.85 4050002 Turnover Turnover 21174589 0 1 9.84E+08 Date date 21174589 0 17615 19435 Time 21174589 0 30649 82126

I don't want to format variables and just want to read date and time variables of this table like hh:mm and dd:mm:yy.

How can I do that?

Accepted Solutions
Solution
‎07-27-2017 05:33 AM
Super User
Posts: 8,083

## Re: How to read data and time as a numeric variable?

How much does the format of the table matter to you?

Why not just have PROC MEANS generate a dataset and then print that?

``````data test;
input id \$ price turnover date time;
format date date9. time time5. ;
cards;
min 0.85 1 17615 30649
max 4050002 9.84E+08 19435 82126
;

proc means noprint data=test;
output out=xx ;
run;

proc print data=xx;
where _stat_='N';
format _all_;
run;

proc print data=xx;
where _stat_ in ('MIN','MAX');
run;``````
```Obs    _TYPE_    _FREQ_    _STAT_    price    turnover    date    time

1        0         2        N         2          2         2       2

Obs    _TYPE_    _FREQ_    _STAT_         price     turnover         date     time

2        0         2       MIN            0.85            1    24MAR2008     8:30
3        0         2       MAX      4050002.00    984000000    18MAR2013    22:48
```

All Replies
Super Contributor
Posts: 266

## Re: How to read data and time as a numeric variable?

Are you asking to have PROC MEANS display the minimum date as 21Jul1969 instead of 3489?

Regular Contributor
Posts: 166

Yes HB.
PROC Star
Posts: 2,344

## Re: How to read data and time as a numeric variable?

Did you paste this table from Excel?

Regular Contributor
Posts: 166

Yes ChrisNZ.
PROC Star
Posts: 2,344

## Re: How to read data and time as a numeric variable?

What you want is called formatting.
Why do you say you don't want it?
Regular Contributor
Posts: 166

## Re: How to read data and time as a numeric variable?

Initial date and time variable were character and I formatted them to
numeric.

attachments. -##
PROC Star
Posts: 2,344

## Re: How to read data and time as a numeric variable?

Just a bit of terminology so we all speak the same language:

Initial date and time variable were character were *converted*  to numeric, and now you want them *formatted* to date and time.

That's done with a format statement. For example:

``format  DATEVAR date9.  TIMEVAR time.;``
Regular Contributor
Posts: 166

## Re: How to read data and time as a numeric variable?

Here is my code which is used to convert character variables to numeric:

data sampledata01;
set sampledata;
trd_event_dt = datepart(trd_event_dt);
format trd_event_dt date9.;
new_var = input(TRD_EVENT_TM,hhmmss.);
format new_var time5.;
drop TRD_EVENT_TM;
rename new_var = TRD_EVENT_TM;
run;

Explanation:
The trd_event_dt is Date.
The TRD_EVENT_TM is Time.
PROC Star
Posts: 2,344

## Re: How to read data and time as a numeric variable?

``````data SAMPLEDATA01;
set SAMPLEDATA;
TRD_EVENT_DT = datepart(TRD_EVENT_DT);
format TRD_EVENT_DT date9. TRD_EVENT_TM time.;
run;``````
Regular Contributor
Posts: 166

PROC Star
Posts: 2,344

## Re: How to read data and time as a numeric variable?

Where did you see a \$ in the code I posted?

\$ formats are for character variables.

PROC Star
Posts: 2,344

## Re: How to read data and time as a numeric variable?

Or do you have a character variable?

What exactly do you have?

Provide: variable name, type, and value, for each of the 2 variables.

Solution
‎07-27-2017 05:33 AM
Super User
Posts: 8,083

## Re: How to read data and time as a numeric variable?

How much does the format of the table matter to you?

Why not just have PROC MEANS generate a dataset and then print that?

``````data test;
input id \$ price turnover date time;
format date date9. time time5. ;
cards;
min 0.85 1 17615 30649
max 4050002 9.84E+08 19435 82126
;

proc means noprint data=test;
output out=xx ;
run;

proc print data=xx;
where _stat_='N';
format _all_;
run;

proc print data=xx;
where _stat_ in ('MIN','MAX');
run;``````
```Obs    _TYPE_    _FREQ_    _STAT_    price    turnover    date    time

1        0         2        N         2          2         2       2

Obs    _TYPE_    _FREQ_    _STAT_         price     turnover         date     time

2        0         2       MIN            0.85            1    24MAR2008     8:30
3        0         2       MAX      4050002.00    984000000    18MAR2013    22:48
```
☑ This topic is solved.