BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
y_fu
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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
y_fu
Fluorite | Level 6

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.

PaigeMiller
Diamond | Level 26

@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
Reeza
Super User

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.


 

 

SuryaKiran
Meteorite | Level 14

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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