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.
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;
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;
It worked! Thank you so much! I've been struggling with this for days.
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.
data want (drop=old:); /*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;
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.
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.
Thank you, this worked. This time I waited until I ran the analyses completely before I responded in case anything else came up.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.