Converting from character to numeric VERSION - character values won't become numeric in new variable

Reply
New Contributor
Posts: 3

Converting from character to numeric VERSION - character values won't become numeric in new variable

[ Edited ]

I can't covert all of my data into numeric in SAS version 9. I cleaned all my data and made the character values numerical manually and now I'm trying to get the proc contents to read it as numerical so I can perform statistical tests. This is the code I have been using

logcode.PNG

I used proc print to check that my data was correct. NEWLOG2 refers to the new variable that is numeric and LOG2 is the original edited variable that now has all numerical values. 1.59 was original included text but won't covert over numerically in the new variable. PLEASE HELP!!!!

newlog.PNG

Trusted Advisor
Posts: 1,631

Re: Converting from character to numeric VERSION - character values won't become numeric in new vari

newlog1 = log1 + 0;
Super User
Posts: 17,960

Re: Converting from character to numeric VERSION - character values won't become numeric in new vari

Your code and words refer to NEWLOG1 but your image shows NEWLOG2 and LOG2?

New Contributor
Posts: 3

Re: Converting from character to numeric VERSION - character values won't become numeric in new vari

Sorry I changed it. Will be doing this for all my character variables.
Super User
Posts: 17,960

Re: Converting from character to numeric VERSION - character values won't become numeric in new vari

Where does your data originate from? A text, XLSX, SAS file?

New Contributor
Posts: 3

Re: Converting from character to numeric VERSION - character values won't become numeric in new vari

REDCAP - then exported into excel - then exported into SAS. Excel is the main source.

Super User
Posts: 17,960

Re: Converting from character to numeric VERSION - character values won't become numeric in new vari

Redcap can generate a CSV file instead - if the file is in the same format everytime and you're doing this often I'd strongly suggest fixing the import step instead of manually recoding things. 

Super User
Super User
Posts: 6,502

Re: Converting from character to numeric VERSION - character values won't become numeric in new vari

Did you get errors from the INPUT() function for the lines that generated missing values?

Either your values are right aligned so that the first 5 characters do not have any value or they have some "invisible" characters that are causing the text to not look like numbers.

First,  use  32.  or even better COMMA32. as your INFORMAT.  (Note BEST is a FORMAT and not an INFORMAT).

Second remove the spaces and/or non digits from the string.

data want ;
  infile cards truncover ;
  input str $char50. ;
  if _n_=1 then str='A0'x||str;
  if _n_=2 then str=strip(str)||'0D'x;
  num1 = input(str,??5.);
  num2 = input(compress(str,'($,ED.)','kd'),??comma32.);
  put num1= best9. @15 num2=best9. @30 str=:$quote.;
cards;
3456
4567
456.123
          5677
1.234E3
(123,345)
;
num1=.        num2=3456      str=" 3456"
num1=.        num2=4567      str="4567
"
num1=456.1    num2=456.123   str="456.123"
num1=.        num2=5677      str="          5677"
num1=1.234    num2=1234      str="1.234E3"
num1=.        num2=-123345   str="(123,345)"

 

Ask a Question
Discussion stats
  • 7 replies
  • 120 views
  • 0 likes
  • 4 in conversation