DATA Step, Macro, Functions and more

convert character to numeric (varying +/- and decimal)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

convert character to numeric (varying +/- and decimal)

Hi community,

 

I want to import a tab delimited file into sas. there are a variable containing percentage values, like 6.9873% or -12.0945%. SAS recognizes this variable as character when I import the file. I need this variable to be numeric, so I did the following:

 

newvar=input(oldvar,percent2.6);

 

However when I look at the data, the code doesn't convert the format consistently, for example:

 

oldvar                                 newvar

12.6545%                          12.6545

9.5451%                            0.095451

 

I appreciate any and all suggestions.


Accepted Solutions
Solution
‎08-29-2017 07:49 PM
Respected Advisor
Posts: 4,927

Re: convert character to numeric (varying +/- and decimal)

@ballardw I think OP had a wider character variable that put the percent sign outside of the informat reach.

PG

View solution in original post


All Replies
Respected Advisor
Posts: 4,927

Re: convert character to numeric (varying +/- and decimal)

Posted in reply to DavyJones

Use newvar=input(oldvar, percent20.);

PG
Super User
Posts: 11,343

Re: convert character to numeric (varying +/- and decimal)

Posted in reply to DavyJones

DavyJones wrote:

Hi community,

 

I want to import a tab delimited file into sas. there are a variable containing percentage values, like 6.9873% or -12.0945%. SAS recognizes this variable as character when I import the file. I need this variable to be numeric, so I did the following:

 

newvar=input(oldvar,percent2.6);

 

However when I look at the data, the code doesn't convert the format consistently, for example:

 

oldvar                                 newvar

12.6545%                          12.6545

9.5451%                            0.095451

 

I appreciate any and all suggestions.


A brief discussion why you get apparently inconsistent results. When an informat is used with decimals as you have then the first digits(preceding the decimal are the overall length of the expected value and with percent you add 2 to account for potenial leading negative sign and the trailing percent sign. Then the digits after the decimal indicate the number digits that should follow the decimal in the input data.

 

I am not sure that you posted the actual code you were running as I get notably different results.

data example;
   x='9.5451%';
   y='12.6545%';
   numx = input(x,percent2.6);
   put numx= best16. ;
   numx2 = input (x,percent9.6);
   put numx2= best16.;
   numy = input(y,percent2.6);
   put numy= best16.;
   numy2 = input (y,percent9.6);
   put numy2= best16.;
run;
Solution
‎08-29-2017 07:49 PM
Respected Advisor
Posts: 4,927

Re: convert character to numeric (varying +/- and decimal)

@ballardw I think OP had a wider character variable that put the percent sign outside of the informat reach.

PG
Super User
Posts: 11,343

Re: convert character to numeric (varying +/- and decimal)


PGStats wrote:

@ballardw I think OP had a wider character variable that put the percent sign outside of the informat reach.


I agree that OP was missing something. I wasn't going to guess too much away from what was shown though and use of any format with 2.6 needed some comment.

Occasional Contributor
Posts: 5

Re: convert character to numeric (varying +/- and decimal)

Thanks for your comments. Yes, you are right. The source of the problem was not converting the percent numbers from character to numeric, but it was importing the data into SAS. Apparantly, as I didn't determine the length of the variable properly, the percentage sign has been removed for some values while kept for the others.

 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 157 views
  • 5 likes
  • 3 in conversation