DATA Step, Macro, Functions and more

Convert column type from character to numeric

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Convert column type from character to numeric

When I import a SAS table, there is a column with date and time like this:

 

date_time

11JUN18:20:19:14

12JUN18:14:04:28

...

etc.

 

This column has type: Date with length $8.

How can I convert this column into a column with type:character?

Since I need to use find function later to separate this data set into several weekly data sets.

The format of output can be just like above or like "11JUN18" without the time.


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 23,980

Re: Convert column type from character to numeric

1. A length of $8 reflects a character variable not a numerical variable, so verify your type vs format vs length. Not sure what makes you think it is numeric. SAS only has two types, numeric and character, a date variable is numeric with a date format.

2. It should be stored as a number with an appropriate format to enable better usage such as date intervals or applying a format to get summary statistics easily.

3. To group your data, a character conversion won't help you since the time will still be different. Rather use a Date format or DATEPART () function to extract only the date portion of the date time variable and use that in your calculations instead.

 


@y_fu wrote:

But the time are all different within the same date.

For example, if I want to analyse data from 10JUN18 to 23JUN2018, what should I do?

I cannot use if then since the time are all different.


 

 

View solution in original post


All Replies
Respected Advisor
Posts: 3,257

Re: Convert column type from character to numeric

[ Edited ]

You can't convert a numeric variable to character, or vice versa.

 

You can create a new variable from your date_time that is character, but your justification is weak ("Since I need to use find function later to separate this data set into several weekly data sets"), and this is not a good thing to do. It would be easier to separate this data into several weekly data sets if you left it as numeric. The INTCK function makes this very simple if you have a numeric variable. I can't even imagine separating data into weekly data sets if it is a character variable.

 

And you'd even be better off not separating the data into different data sets at all and then keep it as one big data set and use a BY statement when you do the analysis.

--
Paige Miller
Occasional Contributor
Posts: 7

Re: Convert column type from character to numeric

Posted in reply to PaigeMiller

But the time are all different within the same date.

For example, if I want to analyse data from 10JUN18 to 23JUN2018, what should I do?

I cannot use if then since the time are all different.

Respected Advisor
Posts: 3,257

Re: Convert column type from character to numeric

[ Edited ]

@y_fu wrote:

But the time are all different within the same date.

For example, if I want to analyse data from 10JUN18 to 23JUN2018, what should I do?

I cannot use if then since the time are all different.


I'm not sure what this means. Why is the time a problem? But nevertheless, the INTCK function makes all of this simple. And I did not suggest you use "if then".

--
Paige Miller
Solution
3 weeks ago
Super User
Posts: 23,980

Re: Convert column type from character to numeric

1. A length of $8 reflects a character variable not a numerical variable, so verify your type vs format vs length. Not sure what makes you think it is numeric. SAS only has two types, numeric and character, a date variable is numeric with a date format.

2. It should be stored as a number with an appropriate format to enable better usage such as date intervals or applying a format to get summary statistics easily.

3. To group your data, a character conversion won't help you since the time will still be different. Rather use a Date format or DATEPART () function to extract only the date portion of the date time variable and use that in your calculations instead.

 


@y_fu wrote:

But the time are all different within the same date.

For example, if I want to analyse data from 10JUN18 to 23JUN2018, what should I do?

I cannot use if then since the time are all different.


 

 

PROC Star
Posts: 624

Re: Convert column type from character to numeric

Importing SAS table or Importing external file into SAS? If your file is Excel and using proc import then, I would suggest you to convert it into CSV and read the data using  DATA step INFILE which you will have more control over the data types.

 

 

Thanks,
Suryakiran
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 85 views
  • 0 likes
  • 4 in conversation