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

Hi, I have the data in excel as below:

Novice__0-1646888573811.png

when I import it with guessingrows the age variable is converted to character. How do I convert it back to numeric with the value of 70+ becoming just a numeric 70? Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Did you import from a text file format such as CSV or Excel/spreadsheet?

If the file is spreadsheet, do a SAVE AS to create CSV. Import that. It will still have problems but you can copy the code from the log that proc import generated. Clean up a bit to remove line numbers or such.

Create a custom informat to read 70+ as 70:

Proc format;
invalue mynum
'70+' = 70
other = [8.]
;
run;

run the format code. In the saved generated data step change the informat to the custom informat such as Mynum. created in proc format. If you haven't used Proc Format much be advised that you cannot name the format or informat with a name ending in digits.

This approach may be best in the long run if you are going to read multiple similar data steps as you save the data step code and change the infile to the next file and the data set name.

 

OR something like this (which could use a similar informat with an Input function call) with if then else code to create a different variable with the numeric value:

data want;
   set have;
   if oldvar='70+' then newvar=70;
   else newvar = input(oldvar, 8.);
run;

where oldvar is the name of the variable imported as character and newvar is the name of a new variable that will be numeric.

 

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Did you import from a text file format such as CSV or Excel/spreadsheet?

If the file is spreadsheet, do a SAVE AS to create CSV. Import that. It will still have problems but you can copy the code from the log that proc import generated. Clean up a bit to remove line numbers or such.

Create a custom informat to read 70+ as 70:

Proc format;
invalue mynum
'70+' = 70
other = [8.]
;
run;

run the format code. In the saved generated data step change the informat to the custom informat such as Mynum. created in proc format. If you haven't used Proc Format much be advised that you cannot name the format or informat with a name ending in digits.

This approach may be best in the long run if you are going to read multiple similar data steps as you save the data step code and change the infile to the next file and the data set name.

 

OR something like this (which could use a similar informat with an Input function call) with if then else code to create a different variable with the numeric value:

data want;
   set have;
   if oldvar='70+' then newvar=70;
   else newvar = input(oldvar, 8.);
run;

where oldvar is the name of the variable imported as character and newvar is the name of a new variable that will be numeric.

 

 

 

Novice_
Fluorite | Level 6

Thanks a lot. It works perfectly 😊

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1244 views
  • 1 like
  • 2 in conversation