Convert characters to numeric

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

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
Wednesday
Occasional Contributor
Posts: 6

Re: Convert characters to numeric

Yes - this is the right solution.  Thank you.

View solution in original post


All Replies
Esteemed Advisor
Posts: 6,646

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
Solution
Wednesday
Occasional Contributor
Posts: 6

Re: Convert characters to numeric

Yes - this is the right solution.  Thank you.

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Convert characters to numeric

In future, please mark the appropriate post as the correct answer, not your reply to it.

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

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.

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

Discussion stats
  • 4 replies
  • 68 views
  • 0 likes
  • 3 in conversation