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

My dataset came from Excel and some columns which should be numbers got converted as characters in SAS.

Apart from one column I want to convert all other character columns into number format. How do I do that?

 

Lets say variables  "x", "y", "z","b", and "a" came in as character variables (Type= Char and format = $7.). Only variable x is okay to be a charcter. How do I keep variable  x as is and conert y, z, and b to Type = Num and format = BEST. in a simple way?

 

Was trying to do something like this, but not working:

 

data new;

set old;

if variable not = 'x'

and format = '$7.'

then format best.;

run;

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
KachiM
Rhodochrosite | Level 12

Edit:

 

We may do it without using array. Borrowing from @singhsahab , we do it as:

 


data have(rename=(_y = y _z=z));
input x $  y $ z $;
_y = input(y, best12.); drop y;
_z = input(z, best12.); drop z;
datalines;
9 1 2
;
run;

 

 

 

View solution in original post

4 REPLIES 4
singhsahab
Lapis Lazuli | Level 10

Hi,

 

While converting variables from character to numeric make sure all the available data under character variable should be number. 

 

data have;
input x $  y $ z $;
datalines;
9 1 2
;
run;
data want;
	set have; 
	array all_n[*] y z ;
	array t(2)  _y _z;
	do i = 1 to dim(all_n); 
		t[i] = input(all_n[i], best.);  
	end;
run; 
proc contents data = want; 
run;
KachiM
Rhodochrosite | Level 12

Edit:

 

We may do it without using array. Borrowing from @singhsahab , we do it as:

 


data have(rename=(_y = y _z=z));
input x $  y $ z $;
_y = input(y, best12.); drop y;
_z = input(z, best12.); drop z;
datalines;
9 1 2
;
run;

 

 

 

ballardw
Super User

@ubshams wrote:

My dataset came from Excel and some columns which should be numbers got converted as characters in SAS.

Apart from one column I want to convert all other character columns into number format. How do I do that?

 

Lets say variables  "x", "y", "z","b", and "a" came in as character variables (Type= Char and format = $7.). Only variable x is okay to be a charcter. How do I keep variable  x as is and conert y, z, and b to Type = Num and format = BEST. in a simple way?

 

Was trying to do something like this, but not working:

 

data new;

set old;

if variable not = 'x'

and format = '$7.'

then format best.;

run;

 

Thanks!

 


This sounds like using Proc Import to bring in the data. Have you verified that all of your actual character variables are correct? Import does this when reading spreadsheets because it only uses a default of the first 20 rows to determine variable type and length. If there are more than one row of headers or blank values in the column you will end up with character values. Or if the values sometimes have non-numeric characters.

 

Better is to learn to control reading the data sets to begin with so you don't have to spend time fixing this. Especially if you will be reading multiple sheets with the same structure. You might get different columns in the next file or sheet to be of the wrong type or length.

 

Either save the sheets as CSV files and write a proper data step to read them (Hint: Proc import with the guessingrows=max option will generate a basic data step to read the file that you can copy from the log to the editor, clean up and reuse by changing infile and data set).

Or use Libname Excel and read the data more directly.

ubshams
Quartz | Level 8

Thanks!

   I have a bunch of ##Div0 in excel in the first 30-40 rows across multiple columns across multiple spreadsheets so the input() option is quicker this time.

But I have noted the guessingrows=max option for future use with csv files.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 8778 views
  • 2 likes
  • 4 in conversation