## char date to numeric conversion

Solved
Regular Contributor
Posts: 214

# char date to numeric conversion

i have a date variable which is in numeric and in "2009-02-17" format. i am calculating flag variable with char date in "2009-02-15" and converting it to numeric like below

data ae053;
set ae052;
format SVSTD date9.;
SVSTD = input ( SVSTD, MMDDYY10.);
if ASTD < SVSTD then flag= "N";
else flag= "Y";
run;

I still get error in the log.

can anyone help me in this

Accepted Solutions
Solution
‎01-17-2017 05:02 AM
Super User
Posts: 9,799

## Re: char date to numeric conversion

Post example test data in the form of a datastep whenever you post a question.  This helps us see the data as you have it.  Otherwise we are guessing.

```data want;
svstd="2009-02-17";
svsstd_num=input(svstd,yymmdd10.);
format svsstd_num date9.;
run;```

Note that you can't put a numeric value back into the character variable (i.e. svstd is character, this will just return a character result.).  Put the result of the conversion into a numeric variable.

All Replies
Super User
Posts: 10,530

## Re: char date to numeric conversion

If svstd is already a SAS date variable (numeric, date format), then you can't use it as argument to the input() function, as that expects a character value.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 214

## Re: char date to numeric conversion

 pt svdt ASTDT flag 101 2009-02-17 2009-02-09 N 102 2009-08-03 2009-08-02 Y 103 2009-02-09 2009-02-10 N

In this case SVDT is in char and format and length as 10.

ASDT is numeric and format and length as 10.

Super Contributor
Posts: 474

## Re: char date to numeric conversion

Hi.

You've got the informat wrong

It should be YYMMDD10., not MMDD10.

Daniel Santos @ www.cgd.pt

Super User
Posts: 10,530

## Re: char date to numeric conversion

vraj1 wrote:
 pt svdt ASTDT flag 101 2009-02-17 2009-02-09 N 102 2009-08-03 2009-08-02 Y 103 2009-02-09 2009-02-10 N

In this case SVDT is in char and format and length as 10.

ASDT is numeric and format and length as 10.

You cannot change the type of a variable, you have to create a new one and remove the old:

``````data have;
input pt svdt :\$10. astdt :yymmdd10. flag :\$1.;
format astdt yymmddd10.;
cards;
101 2009-02-17 2009-02-09 N
102 2009-08-03 2009-08-02 Y
103 2009-02-09 2009-02-10 N
;
run;

data want;
set have (rename=(svdt=svdt_old));
format svdt yymmddd10.;
svdt = input(svdt_old,yymmdd10.);
drop svdt_old;
run;

proc print data=want;
run;``````

The result looks like this:

```Obs     pt         astdt    flag          svdt

1     101    2009-02-09     N      2009-02-17
2     102    2009-08-02     Y      2009-08-03
3     103    2009-02-10     N      2009-02-09
```
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎01-17-2017 05:02 AM
Super User
Posts: 9,799

## Re: char date to numeric conversion

Post example test data in the form of a datastep whenever you post a question.  This helps us see the data as you have it.  Otherwise we are guessing.

```data want;
svstd="2009-02-17";
svsstd_num=input(svstd,yymmdd10.);
format svsstd_num date9.;
run;```

Note that you can't put a numeric value back into the character variable (i.e. svstd is character, this will just return a character result.).  Put the result of the conversion into a numeric variable.

Super User
Posts: 10,530

## Re: char date to numeric conversion

And, for the future:

"I still get error in the log."

is NOT helpful. Post at least the ERROR message text, but much better is to post the log including the whole step where the ERROR happened.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 214