BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I want to convert all char variables into numeric.

In this case all char variables have digits so it is possible.

What is the reason that in want data set I dont get numeric columns?

 Data have;
 input mon1 $ mon2 $ mon3 $ mon4 $ mon5 $ mon6 $;
 cards;
 2301 2302 2303 2303 2305 2306
;
 Run;

DATA want;
SET have;
ARRAY _char mon:;
ARRAY _num mon:;
DO i=1  to dim(_char);
_num{i} = input(_char{i},best.);
END;
RUN; 
5 REPLIES 5
A_Kh
Lapis Lazuli | Level 10

You are referring to the same variables both as character and numeric, and a SAS variable cannot be of both datatype at the same time. 

You need to get new names for numeric vars, like _mon1-_mon6. 

Ronein
Meteorite | Level 14

I have tried by your answer but got an error


 Data have;
 input mon1 $ mon2 $ mon3 $ mon4 $ mon5 $ mon6 $;
 cards;
 2301 2302 2303 2303 2305 2306
;
 Run;

DATA want;
SET have;
ARRAY _char mon:;
ARRAY _num _mon:;
DO i=1  to dim(_char);
_num{i} = input(_char{i},best.);
END;
RUN; 

The error

ERROR: Array subscript out of range at line 39 column 17.
mon1=2301 mon2=2302 mon3=2303 mon4=2303 mon5=2305 mon6=2306 _I_=. i=1 _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.HAVE.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0 observations and 7 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
A_Kh
Lapis Lazuli | Level 10

ARRAY _num _mon1-_mon6;

 

Rename should be done outside the array

Tom
Super User Tom
Super User

You cannot use the : suffix to match variable names of variables that do not yet exist.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 809 views
  • 0 likes
  • 4 in conversation