Discover More Tips and Techniques on This Blog

How to detect missing values using ARRAYS

Using an array in SAS to detect missing values

Direct link: http://ssc.utexas.edu/consulting/answers/sas/sas65.html

Question:
How do I exclude observations from my PROC FREQ analysis when a value is missing from a list of variables?

Answer:
In the SAS DATA step, you can create a new variable ("miss" in the example below) that is set equal to 1 when a variable has a missing value, 0 otherwise. Use the ARRAY statement and a DO loop to check for missing values across a list of variables; the syntax is:

DATA one ;
INFILE xxx;
INPUT a b c d e;
miss=0;
ARRAY vv(5) a b c d e ;
DO i=1 TO 5 ;
IF vv(i)=. THEN DO;
miss=1 ;
i=5;

END;
END;
RUN;


PROC FREQ;
WHERE miss =0;
TABLES a b c d e ;
RUN ;

Here, the array "vv" has 5 elements (a,b,c,d,e), and the loop "i" is likewise set to 5.

For each observation, the loop iterates 5 times, checking for missing values across the list of 5 variables. When a missing value is encountered, the variable "miss" is set to 1 and the loop stopped for that observation.

"Miss" was initially set to zero, and it is only changed if an observation has missing data on any of the five variables. The PROC FREQ then uses the WHERE statement to restrict processing to observations having "miss" set to zero.
Want to know more about Missing Values...

1 comment:

  1. Hi,
    Um..I have a question. When I delete "i=5" in the second doloop, it works as same as before deleting.
    I don't know exactly about i=5....

    ReplyDelete

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.