BookmarkSubscribeRSS Feed
awasser
Calcite | Level 5

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;

 

3 REPLIES 3
novinosrin
Tourmaline | Level 20
 

 

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;

 

Kurt_Bremser
Super User

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   
awasser
Calcite | Level 5

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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 690 views
  • 1 like
  • 3 in conversation