Hi, I try to create three new indicator columns. These indicators are character filed. When I run my code, I got the below message. It seems SAS converted these fields to numeric value. What should I do?
Thanks
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
21:22 22:28
NOTE: Invalid numeric data, 'Y' , at line 22 column 36.
NOTE: Invalid numeric data, 'Y' , at line 22 column 36.
DATA TEST;
SET ABC;
ARRAY OLD (3) INDICATOR1 INDICATOR2 INDICATOR3;
ARRAY NEW (3) INDICATOR1_NEW INDICATOR2_NEW INDICATOR3_NEW;
DO I = 1 TO 3;
IF OLD(I) = "N" THEN NEW(I)= " ";
ELSE IF OLD(I) NE "N" THEN NEW(I) =OLD(I);
END;
DROP I;
RUN;
It would probably be easiest just to assign them all the same max length of $2. If the actual length becomes an issue you can specify a Length statment for the new variables before putting them in the Array definition such as
Length INDICATOR1_NEW $ 1 INDICATOR2_NEW INDICATOR3_NEW $ 2;
BTW I would recommend using New_Indicator1, New_Indicator2 etc. For a number of reasons. It is easy to specify variable lists such as New_I: to get all variables that start with New_I for almost anything that accepts a list of variable names.
For instance your array definition could be
Array new (3) $2 New_Indicator1 - New_Indicator3;
Which when you get to a project with 25, 50 or more variables makes things a whole lot easier to change that 3 to 25 and be done instead of typing in all 25 variable names.
When you created the array New the variables by default were created as numeric if they did not exist in the data set ABC.
ARRAY NEW (3) $1 INDICATOR1_NEW INDICATOR2_NEW INDICATOR3_NEW;
would create character variables of length 1. if you need longer than increase the 1 to the needed length.
Thanks Ballardw. It works now!
What happen if INDICATOR1_NEW has one character, where INDICATOR2_NEW and INDICATOR3_NEW have two characters?
It would probably be easiest just to assign them all the same max length of $2. If the actual length becomes an issue you can specify a Length statment for the new variables before putting them in the Array definition such as
Length INDICATOR1_NEW $ 1 INDICATOR2_NEW INDICATOR3_NEW $ 2;
BTW I would recommend using New_Indicator1, New_Indicator2 etc. For a number of reasons. It is easy to specify variable lists such as New_I: to get all variables that start with New_I for almost anything that accepts a list of variable names.
For instance your array definition could be
Array new (3) $2 New_Indicator1 - New_Indicator3;
Which when you get to a project with 25, 50 or more variables makes things a whole lot easier to change that 3 to 25 and be done instead of typing in all 25 variable names.
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.