Hello folks,
I have a problem that I have no been able to solve. Basically, I want to recode the character R as the numeric missing value 999 but keep getting the error "invalid numeric data" error. Thanks for any help!
*Recode missing values;
DATA autonomy; SET autonomy;
ARRAY old(16) $16 aauto1-aauto8 mauto1-mauto8;
ARRAY recode(16) aauto1-aauto8 mauto1-mauto8;
DO i = 1 to 16 BY 1;
IF old{i}='R' THEN recode{i}=999;
END;
DROP i;
RUN;
DATA autonomy; SET autonomy;
ARRAY old(16) $16 aauto1-aauto8 mauto1-mauto8;
DO i = 1 to 16 BY 1;
IF old{i}='R' THEN old{i}='999';
END;
DROP i;
RUN;
Your code will work if your variables are of type character:
data autonomy;
input (aauto1-aauto8 mauto1-mauto8) ($);
cards;
1 2 3 4 R 6 7 8 1 2 3 4 R 6 7 8
;
run;
DATA autonomy; SET autonomy;
ARRAY old(16) $16 aauto1-aauto8 mauto1-mauto8;
ARRAY recode(16) aauto1-aauto8 mauto1-mauto8;
DO i = 1 to 16 BY 1;
IF old{i}='R' THEN recode{i}=999;
END;
DROP i;
RUN;
But if your variables are in fact numeric, and just have a format applied to them that displays the character R, you get the invalid numeric data error:
proc format;
value myform
999 = 'R'
other=[best10.]
;
run;
data autonomy;
input aauto1-aauto8 mauto1-mauto8;
format aauto1-aauto8 mauto1-mauto8 myform.;
cards;
1 2 3 4 999 6 7 8 1 2 3 4 999 6 7 8
;
run;
proc print data=autonomy noobs;
run;
DATA autonomy; SET autonomy;
ARRAY old(16) $16 aauto1-aauto8 mauto1-mauto8;
ARRAY recode(16) aauto1-aauto8 mauto1-mauto8;
DO i = 1 to 16 BY 1;
IF old{i}='R' THEN recode{i}=999;
END;
DROP i;
RUN;
See the output from the proc print:
aauto1 aauto2 aauto3 aauto4 aauto5 aauto6 aauto7 aauto8 mauto1 mauto2 mauto3 mauto4 mauto5 mauto6 mauto7 mauto8 1 2 3 4 R 6 7 8 1 2 3 4 R 6 7 8
Thanks all. After trying everyone's suggestion, the problem still persists. I know a crude way of solving it, so I'm going to go with that for now.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.