BookmarkSubscribeRSS Feed
sbrent
Calcite | Level 5

I am trying to change data type from numeric to character in my SAS table.  I keep getting an error and I don't know what's going on.  I found a previous post that was helpful but hasn't solved my issue.  It is telling me my character values have been converted to numeric which is the opposite of what I want to do and, there aren't any character values that are numbers in the table so it's not actually doing anything.  It's also telling me that my numbers are already numeric...yeah I know, I want to change that.   Below is my code and the log.  Any suggestions would be greatly appreciated.  Thank you.  Shannan

 

CODE

data other_costs_char;

set other_costs;

 

   length nvar1-nvar234 12;

   format nvar1-nvar234 $12.;

 

     array num _numeric_;

     array _char(*) $12 nvar1-nvar234;

         do i=1 to dim(num);

         _char(i) = put(num(i), best12.);

   end;

 

run;

 

LOG 

WARNING: Variable nvar227 has already been defined as numeric.

WARNING: Variable nvar228 has already been defined as numeric.

WARNING: Variable nvar229 has already been defined as numeric.

WARNING: Variable nvar230 has already been defined as numeric.

WARNING: Variable nvar231 has already been defined as numeric.

WARNING: Variable nvar232 has already been defined as numeric.

WARNING: Variable nvar233 has already been defined as numeric.

WARNING: Variable nvar234 has already been defined as numeric.

992

993 array num _numeric_;

994 array _char(*) $12 nvar1-nvar234;

995 do i=1 to dim(num);

996 _char(i) = put(num(i), best12.);

997 end;

998 run;

NOTE: Character values have been converted to numeric values at the places given by:

(Line):(Column).

996:21

NOTE: The SAS System stopped processing this step because of errors.

WARNING: The data set WORK.OTHER_COSTS_CHAR may be incomplete. When this step was stopped there

were 0 observations and 471 variables.

NOTE: DATA statement used (Total process time):

real time 0.09 seconds

cpu time 0.07 seconds

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

Can you link to the previous post?

 

Also, this is contradictory.. Are these variables numeric or character?

 

length nvar1-nvar234 12;
format nvar1-nvar234 $12.;
Kurt_Bremser
Super User

The problem starts here:

 76            length nvar1-nvar234 12;
                                    __
                                    352
 ERROR 352-185: The length of numeric variables is 3-8.

Here, you define your variables as numeric, albeit with an invalid length. Numeric variables in SAS have a maximum of 8 bytes for real number storage. But this still results in 234 numeric variables.

Then, in

 77            format nvar1-nvar234 $12.;
 WARNING: Variable nvar1 has already been defined as numeric.
 

you try to assign a character format to those numeric variables, which is not possible.

If you actually wanted to create character variables, omit the length statement completely. The format statement will suffice.

 

I suggest you transpose your data to a long format, convert the resulting single variable to character, and transpose back (unless keeping a long format is not better in the first place).

sbrent
Calcite | Level 5

Thank you!  I have removed my contradictory data type statements (removed the length statement and just left the format statement).

 

I'm getting a new error...

ERROR: Array subscript out of range at line 1037 column 21.

 

After which is lists ALL the data from both arrays.  I'm not sure what it means by line 1037.  The data has 39 rows and 236 columns. Then it has more error messages.

 

_I_=. i=235 _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.OTHER_COSTS.

WARNING: The data set WORK.OTHER_COSTS_CHAR may be incomplete. When this step was stopped there

were 0 observations and 471 variables.

WARNING: Data set WORK.OTHER_COSTS_CHAR was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

real time 0.03 seconds

cpu time 0.03 seconds

 

To answer the question, it has been transposed and that is the final format it needs to be exported into.  I'm trying to convert the dollar amounts in the transposed table to character so they can be combined with another table that contains header rows with descriptions.  Then the whole thing will be exported as one table into Excel where the numbers will once again become numbers.

 

I know, convoluted...but learning to convert in an array seemed faster than learning how to use all the various report functions that could probably accomplish the same/similar end result...and I of course have a deadline.

sbrent
Calcite | Level 5
Forgot to add that after transpose, all the nulls are replaced with 0, just to give a clearer picture of my data.
sbrent
Calcite | Level 5

It's running without out error (I counted my variables wrong, was one off).

 

However, when I open the new table that it creates, all my variables are still numeric.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 1690 views
  • 0 likes
  • 3 in conversation