DATA Step, Macro, Functions and more

Convert string variable to numeric

Accepted Solution Solved
Reply
Occasional Contributor kvc
Occasional Contributor
Posts: 13
Accepted Solution

Convert string variable to numeric

I've been trying to figure out how to convert a string variable to numeric in my dataset. The dataset was originally in SPSS, where I changed the variable API08 from string to numeric. However, after saving the dataset in SAS, the variable continues to be read as string when I try to conduct PROC REG. Here is the error message:

ERROR: Variable API08 should be either numeric or in CLASSES list.

This variable is an academic performance index ranging from 200 to 1000. The variable is already a number.

After searching on Google for some kind of code (couldn't find it in my SAS book), I found this code and example:

CODE:

numericVar = input(charVar, informat.);

EXAMPLE:

data = dataset;

charVar='1234';

numericvar=input(charvar,4.0);

run;

My question - how do I take that code and write it for my variable? Thanks for considering my question.


Accepted Solutions
Solution
‎10-29-2011 09:21 PM
PROC Star
Posts: 7,473

Convert string variable to numeric

Almost exactly the way shown in your example.  Say your current SAS file was called "have" and that you didn't want to risk messing it up, thus wanted to create a new dataset called "want".  And, you ets say that you didn't want to change the name.  I'd accomplish it as follows:

data want (drop=old);

  set have (rename=(AP108=old));

   AP108=input(old,4.0);

run;

View solution in original post


All Replies
Solution
‎10-29-2011 09:21 PM
PROC Star
Posts: 7,473

Convert string variable to numeric

Almost exactly the way shown in your example.  Say your current SAS file was called "have" and that you didn't want to risk messing it up, thus wanted to create a new dataset called "want".  And, you ets say that you didn't want to change the name.  I'd accomplish it as follows:

data want (drop=old);

  set have (rename=(AP108=old));

   AP108=input(old,4.0);

run;

Occasional Contributor kvc
Occasional Contributor
Posts: 13

Re: Convert string variable to numeric

It worked! Thank you so much! I've been struggling with this for days.

Occasional Contributor kvc
Occasional Contributor
Posts: 13

Re: Convert string variable to numeric

I have a tag-on question. It looks like there is another string variable that also needs to be converted. Can I do it in the same step as API08? Or do I have to create another dataset for the second variable. The second variable is VALID08, which ranges from 98 to 1100. Thanks for your help.

PROC Star
Posts: 7,473

Re: Convert string variable to numeric

data want (drop=oldSmiley Happy;  /*This now drops any variable that starts with the string old*/

  set have (rename=(API08=oldAPI VALID08=oldVALID));

   API08=input(oldAPI,4.0);

   VALID08=input(oldVALID,4.0);

run;

Occasional Contributor kvc
Occasional Contributor
Posts: 13

Re: Convert string variable to numeric

Once again, it worked, and I thank you.

One last thing. It turns out that there were a total of 5 variables that needed to be converted, and I just added them to the code as indicated above. All 5 variables were converted, and the old ones dropped, which is great.

However, one of the variables had two decimal places (it ranged from 1 to 5, so values could be 1.77 and so forth). The variable is AVG_ED08. I tried adding two zeroes after the period (see below)

AVG_ED08=input(oldAVG_ED08,1.00);

But the two decimal places were dropped in the new file. Can you please tell me the code for retaining the two decimal places? Thanks again.

Super User
Super User
Posts: 7,046

Re: Convert string variable to numeric

Change the format used to convert the character string to a number in the INPUT function call.

For formats and informats the number before the period is the total length and the number after the period is the number of decimal places.  But for informats if the text has a decimal in it then it will use it when reading the data and ignores the number of decimals when in the informat.

Art's code uses 4.0 as the informat which will handle character strings up to 4 characters long.

Your code uses 1.0 which will handle only a single character.

I would recommend using 12. as the informat which is the normal length SAS uses for auto conversion between text and numbers.

Read up about formats and informats in the manuals.

Occasional Contributor kvc
Occasional Contributor
Posts: 13

Re: Convert string variable to numeric

Thank you, this worked. This time I waited until I ran the analyses completely before I responded in case anything else came up.

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 31602 views
  • 6 likes
  • 3 in conversation