Learning SAS? Welcome to the exclusive online community for all SAS learners.

Converting character to numerical values

Reply
New Contributor
Posts: 3

Converting character to numerical values

Does anyone know how to cover character values to numeric values so that I can run analysis? All my data when I imported it was character values on SASUniversity. I have tried different methods and none of which seem to work. Thanks.

Occasional Contributor
Posts: 9

Re: Converting character to numerical values

Hi,

You can use "input" function.

new=input(var, informat);

Super User
Super User
Posts: 7,973

Re: Converting character to numerical values

SanjayAhir is quite correct, input for char to num, put for num to char.  However one thing I would add is to check your import.  What are you importing, an Excel file, delimited, datalines?  Depending on application you can set guessrows larger for Excel, or use a datastep to read in the data in the required format straight away.

New Contributor
Posts: 3

Re: Converting character to numerical values

I Am importing a csv file.

so I would type in the code:


new=input(var, informat);


so my my variables I would use the code each time for every variable. Say my variable was called age and my file was called Megandatamerge1.csv I would ente. The following code:


data= megandatamerge.csv;

new=input (age,informat);

run;

Super User
Super User
Posts: 7,973

Re: Converting character to numerical values

Set this on your import then:

data want;

     attrib var1 var2 format=$20. var3 var4 format=best.;

     infile "...\xyz.csv" dlm=',' dsd missover;

     input var1 $ var2 $ var3 var4;

run;

No need to convert.

New Contributor
Posts: 3

Re: Converting character to numerical values

This is the code I did and didn't work.

data want;

     attrib FoodSR CAff format=$20. Sugar Carb format=best.;

     infile "datamerge1.csv" dlm=',' dsd missover;

     input FoodSR $ CAff $ Sugar Carb;

run;

Super User
Super User
Posts: 7,973

Re: Converting character to numerical values

Sorry, you will have to give a bit more info.  What didn't work, it didn't import at all?  Warnings/Errors, file was created but all text?  Maybe post the file itself.  Does the numeric fields contain any non-numeric information?

Occasional Contributor
Posts: 9

Re: Converting character to numerical values

What error is coming while doing this. can you pls paste the log here.

Occasional Contributor
Posts: 9

Re: Converting character to numerical values

Hi hoelmk,

what kind of file are you importing or which procedure are you using to import??

I meant to say you are importing excel file, CSV file or any other format and for that you are using import wizard or PROC IMPORT procedure or any other way

Ask a Question
Discussion stats
  • 8 replies
  • 520 views
  • 0 likes
  • 3 in conversation