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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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