## Convert characters to numeric

Solved
Occasional Contributor
Posts: 15

# Convert characters to numeric

Hello,

I have input data that looks like these -

Size         Space     Date

840K        7%          2017.183

23433K    25%        2013.034

The "K" probably is 1000 or 1024 - I take the value as 1000.

I have not seen any INFORMATS that can convert 840K to 840000 (numeric value) or 7% to 0.07 (numeric value).  The date has a "." in between the year and day (in Julian format).  I need to convert the date to SAS internal date.  Again, I went looking at the INFORMATS available - but, I can't see any suitable ones.

I understand the coding required (for example) -

SIZEN = INPUT (SIZE. informat);

It is trying to look for a proper informat that gives me the problems.

Thank you.

TS

Accepted Solutions
Solution
‎07-19-2017 07:26 AM
Occasional Contributor
Posts: 15

## Re: Convert characters to numeric

Yes - this is the right solution.  Thank you.

All Replies
Super User
Posts: 9,855

## Re: Convert characters to numeric

``````data want;
infile cards firstobs=2;
format size 10. space 7.2 date date9.;
input size_string \$ space :percent7. date_string :\$10.;
date = input(compress(date_string,'.'),julian8.);
pos = notdigit(trim(size_string));
size = input(substr(size_string,1,pos-1),10.);
select (substr(size_string,pos,1));
when ('K') size = size * 1024;
end;
drop size_string date_string pos;
cards;
Size         Space     Date
840K        7%          2017.183
23433K    25%        2013.034
;
run;``````

You just have to "fit" the data for the proper formats. How eg the input for the JULIANw. format has to look can be taken from the documentation.

The documentation can usually be found by googling "SAS 9.4" and the keyword, or something like "SAS 9.4 informats by category".

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎07-19-2017 07:26 AM
Occasional Contributor
Posts: 15

## Re: Convert characters to numeric

Yes - this is the right solution.  Thank you.

Super User
Posts: 9,376

Super User
Posts: 9,376

## Re: Convert characters to numeric

Well, the first is simple:

```sizen=input(tranwrd(size,"Z","000"),best.);
```

The date:

```new_date=input(compress(date,"."),julian7.);
```
☑ This topic is solved.