Solved
Contributor
Posts: 43

# Convert Charc to numeric

Hello One more doubt please guide-

I have a variable name 'Tdate'   2012-06-23 00:00:00 , this is character variable and I want to convert it to numeric with just a datepart

I use input function but it did not work

Query is like and it is in the data step

if '01Jan2011'd  <= tdate <= '22Jan2011'd

Thank you

Accepted Solutions
Solution
‎02-10-2014 05:04 PM
Super User
Posts: 6,785

## Re: Convert Charc to numeric

If you are only interested in subsetting, you could try:

if '01Jan2011'd <= input(tdate, yymmdd10.) <= '22Jan2011'd;

If you are interested in actually creating a variable that equals the date portion of TDATE on SAS's numeric date scale, you could use:

date_portion = input(tdate, yymmdd10.);

format date_portion date9.;

All Replies
Super User
Posts: 23,776

## Re: Convert Charc to numeric

Perhaps post what you tried.

Solution
‎02-10-2014 05:04 PM
Super User
Posts: 6,785

## Re: Convert Charc to numeric

If you are only interested in subsetting, you could try:

if '01Jan2011'd <= input(tdate, yymmdd10.) <= '22Jan2011'd;

If you are interested in actually creating a variable that equals the date portion of TDATE on SAS's numeric date scale, you could use:

date_portion = input(tdate, yymmdd10.);

format date_portion date9.;

Super User
Posts: 13,583

## Re: Convert Charc to numeric

I think those inputs need to use the DATE9. informat, not yymmdd;

Occasional Contributor
Posts: 8

## Re: Convert Charc to numeric

Thank You. I am trying same kind of thing.I Got it.

Valued Guide
Posts: 2,191

## Re: Convert Charc to numeric

```
Query is like and it is in the data step

if '01Jan2011'd  <= tdate <= '22Jan2011'd
```

If you wish to filter your dates you would make fewer conversions (and so get better performance) by converting just the range-end dates

if

"%sysfunc(inputn( 01jan2011, date9), yymmddD10)"

<= tdate <=

"%sysfunc( inputn( 22jan2011, date9), yymmddD10) 23:59:59.99"

;

where tdate is stored as a string as you describe above.

Super User
Posts: 13,583

## Re: Convert Charc to numeric

```Dipu wrote:

I have a variable name 'Tdate'   2012-06-23 00:00:00 , this is character variable and I want to convert it to numeric with just a datepart

I use input function but it did not work
Query is like and it is in the data step
if '01Jan2011'd  <= tdate <= '22Jan2011'd
Thank you
```

You have two simple basic choices other than reimporting the data.

Create a new date variable to use instead of tdate using something like

Date= input(tdate,Date9.);

or place the conversion in the comparison as Astounding mentioned, using the DATE9. informat.

If this were my data, I would be strongly tempted to go back to the data import part and read any date, time or datetime values as SAS date, time or datetime variables since there are many things that are easily done with the SAS date type variables that become a pain when left as text. Some examples are determining intervals between dates, advancing a date a given number of days, week, months and other periods, determining the day of the week or the week of a year, and creative use of formats will allow summaries of many time intervals by using the right format in one of the summary procedures.

Contributor
Posts: 43

## Re: Convert Charc to numeric

Thank you All for helping Jaison with the Query.

Dipu

🔒 This topic is solved and locked.