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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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