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;
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.
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.
ARRAY _num _mon1-_mon6;
Rename should be done outside the array
You cannot use the : suffix to match variable names of variables that do not yet exist.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.