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

Hello,

 

I have 10 variables in my dataset that are character.  9 of them should be numeric.  Is there a solution using array that would convert the 9 to numeric? like:

 

array xx _character_;

do over xx;

     <if variable is not what I want, leave it alone, else convert to numeric>;

end;

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Sure, use VNAME() to get the variable name. But you only have 10 variables, it's just as easy to list them, if the variable you want to exclude is first or last it's much easier as well. 

 

Note that when doing type conversion in SAS you need to store them in a new variable so you'll have to create the new variable names anyways. In that case you may as well just list them. 

Or it's usually easier to go back and fix your import, if that was the issue, and read in the data correctly the first time. 

 

data want;
set have;

array old(*) $ _character_;
array new(*) $ new_varname1 new_varname2 ..... new_varname10;

do i=1 to dim(old);
if vname(old(i)) ne 'VariableName2Exclude' then new(i) = input(old(i), 8.); end; run;

 

View solution in original post

3 REPLIES 3
Reeza
Super User

Sure, use VNAME() to get the variable name. But you only have 10 variables, it's just as easy to list them, if the variable you want to exclude is first or last it's much easier as well. 

 

Note that when doing type conversion in SAS you need to store them in a new variable so you'll have to create the new variable names anyways. In that case you may as well just list them. 

Or it's usually easier to go back and fix your import, if that was the issue, and read in the data correctly the first time. 

 

data want;
set have;

array old(*) $ _character_;
array new(*) $ new_varname1 new_varname2 ..... new_varname10;

do i=1 to dim(old);
if vname(old(i)) ne 'VariableName2Exclude' then new(i) = input(old(i), 8.); end; run;

 

jffeudo86
Quartz | Level 8
I learned a new thing today, vname. Thank you!
ballardw
Super User

If you wanted the variables to be numeric why were they character to begin with?

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

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
  • 3 replies
  • 1679 views
  • 1 like
  • 3 in conversation