BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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
Barite | Level 11

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
Onyx | Level 15

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
Barite | Level 11

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.

hackathon24-white-horiz.png

Join the 2025 SAS Hackathon!

Calling all data scientists and open-source enthusiasts! Want to solve real problems that impact your company or the world? Register to hack by August 31st!

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
  • 1358 views
  • 0 likes
  • 4 in conversation