Hi all. I was passed along a program that contains the following data step. It recodes ALL missing numeric values to 0.
But I don't understand how it's doing it? In particular I don't understand the keywords 'change', '_numeric_', and 'do over'. Can anyone help me understand how this code works? Any pointers would be most appreciated.
/* RECODE ALL NUMBERIC MISSING VALUES FROM . TO 0 */
DATA WANT;
SET HAVE;
ARRAY CHANGE _NUMERIC_;
DO OVER CHANGE;
IF CHANGE=. THEN CHANGE=0;
END;
RUN;
Also, do you feel this is a good way of doing this task or are there better alternatives?
HI @buechler66
1. First off, do over array is an implicit array. Most people would suggest not to use implicit array as it's been deprecated.
2. The implicit array creates an automatic index variable _i_ in the PDV , however the index variable is not written to the output dataset.
3. _numeric_ is a keyword for grouping all numeric variable as elements in your array "change". So once your array is declared at compile time, sas groups the num vars with a non scalar value for the array identifier.
4. What actually happens is
Your
DO OVER CHANGE;
IF CHANGE=. THEN CHANGE=0;
END;
resolves to
do _i_=1 to dim(change); /*dim(change) is total number of num elements(vars) that's been grouped*/
if change(_i_)=. then change(_i_)=0;
end;
I hope this helps.
Best regards!
@buechler66 wrote:
Hi all. I was passed along a program that contains the following data step. It recodes ALL missing numeric values to 0.
But I don't understand how it's doing it? In particular I don't understand the keywords 'change', '_numeric_', and 'do over'. Can anyone help me understand how this code works? Any pointers would be most appreciated.
/* RECODE ALL NUMBERIC MISSING VALUES FROM . TO 0 */ DATA WANT; SET HAVE; ARRAY CHANGE _NUMERIC_; DO OVER CHANGE; IF CHANGE=. THEN CHANGE=0; END; RUN;
Also, do you feel this is a good way of doing this task or are there better alternatives?
That way of doing it works.
here is a link to the same code you have provided that has the solution.
HI @buechler66
1. First off, do over array is an implicit array. Most people would suggest not to use implicit array as it's been deprecated.
2. The implicit array creates an automatic index variable _i_ in the PDV , however the index variable is not written to the output dataset.
3. _numeric_ is a keyword for grouping all numeric variable as elements in your array "change". So once your array is declared at compile time, sas groups the num vars with a non scalar value for the array identifier.
4. What actually happens is
Your
DO OVER CHANGE;
IF CHANGE=. THEN CHANGE=0;
END;
resolves to
do _i_=1 to dim(change); /*dim(change) is total number of num elements(vars) that's been grouped*/
if change(_i_)=. then change(_i_)=0;
end;
I hope this helps.
Best regards!
@buechler66 wrote:
Hi all. I was passed along a program that contains the following data step. It recodes ALL missing numeric values to 0.
But I don't understand how it's doing it? In particular I don't understand the keywords 'change', '_numeric_', and 'do over'. Can anyone help me understand how this code works? Any pointers would be most appreciated.
/* RECODE ALL NUMBERIC MISSING VALUES FROM . TO 0 */ DATA WANT; SET HAVE; ARRAY CHANGE _NUMERIC_; DO OVER CHANGE; IF CHANGE=. THEN CHANGE=0; END; RUN;
Also, do you feel this is a good way of doing this task or are there better alternatives?
You are welcome. Have a nice day!
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.