BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kvc
Calcite | Level 5 kvc
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

7 REPLIES 7
art297
Opal | Level 21

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;

kvc
Calcite | Level 5 kvc
Calcite | Level 5

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

kvc
Calcite | Level 5 kvc
Calcite | Level 5

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.

art297
Opal | Level 21

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;

kvc
Calcite | Level 5 kvc
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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.

kvc
Calcite | Level 5 kvc
Calcite | Level 5

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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