Help using Base SAS procedures

How do you use an array to convert all numeric to missing?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

How do you use an array to convert all numeric to missing?

Hello SAS community,

I'm wanting to know how to code an array that sets all numeric responses from all variables to missing.

The data set has more than 100 variables, so I would like to know a command/syntax that would allow me to have the array go through all the variables and change a numeric response to missing. I understand the array statement needs to change to do this and I've been able to find how to change the syntax for character using " _character_" , but not for all numeric values.

Below is sample code:

data new;

set old.library;

array numarray {3} length width height;

do i=1 to 3;

if numarray {i}= 999 then numarray {i}= .;

end;

drop i;

run;


Accepted Solutions
Solution
‎01-04-2015 10:46 AM
Super Contributor
Posts: 298

Re: How do you use an array to convert all numeric to missing?

Here is another example.

data want;

   set sashelp.class;

   array k

  • _numeric_;
  •    do i = 1 to dim(k);

          k = .;

       end;

    drop i;

    run;

    View solution in original post


    All Replies
    Trusted Advisor
    Posts: 1,228

    Re: How do you use an array to convert all numeric to missing?

    Hi,

    You can do it using _numeric_ for numeric variables. Make sure you have only variables in the data set which are required to be imputed as missing otherwise it will replace all the numeric variables in the data set with missing. Please see below the syntax:

    data new;

    set old.library;

    array numarray {*} _numeric_;

    do i=1 to dim(numarray);

    if numarray {i}= 999 then numarray {i}= .;

    end;

    drop i;

    run;

    Occasional Contributor
    Posts: 13

    Re: How do you use an array to convert all numeric to missing?

    Thank you,

    Just to clarify what do you mean the variables all have to be imputed as missing, so that all the numerical values are changed to missing. I believe you mean that the values for all the variables have to have for example a designated number such as, 999, as a response like in the above syntax in order to be changed to missing?

    Is this the thinking? And I guess if a response doesn't then it would make all responses numeric. So, if not all variables have missing coded numerically in this way, I would have to code for missing "." in ranges of variables?

    My data set is a survey.

    Thank you!

    Trusted Advisor
    Posts: 1,228

    Re: How do you use an array to convert all numeric to missing?

    This was just based on your coded example. In survey data this is a common practice to code don't know/refused responses as 999. Sometimes survey data also contains some demo questions like age, income, region etc., so if income variable has a value 999 that will also be imputed as missing as well and may impact the analysis.

    Occasional Contributor
    Posts: 13

    Re: How do you use an array to convert all numeric to missing?

    I see! Thank you. I was looking at all the variables before putting in the code to change in the data set how missing is displayed and it is displayed as 996 or "missing".

    I find it tedious to see what variables exactly are which for missing or 996 specifically. 996 throughout the data set is representative for missing and not anything else, so it won't impact my analysis I assume.

    I ultimately want to create a missing data profile using the MI procedure, since my data set contains a lot of missing. The Missing data profile will then provide information to do multiple imputations for data analysis.

    I believe the MI procedure by default assumes "." as missing, so I'm trying to change 996 and missing in the variables to ".".

    Could I create a character and numeric array and have these two arrays account for all the data to convert all the data to "." for missing?

    Then I could do the MI procedure to see the missing data pattern?

    Thank you.

    Trusted Advisor
    Posts: 1,228

    Re: How do you use an array to convert all numeric to missing?

    As you have already figured it out how to apply syntax for character using _character_ so just define two arrays in data step one for character and other for numeric to get the desired results.

    Solution
    ‎01-04-2015 10:46 AM
    Super Contributor
    Posts: 298

    Re: How do you use an array to convert all numeric to missing?

    Here is another example.

    data want;

       set sashelp.class;

       array k

  • _numeric_;
  •    do i = 1 to dim(k);

          k = .;

       end;

    drop i;

    run;

    Super User
    Posts: 10,035

    Re: How do you use an array to convert all numeric to missing?

    You don't need array.

    call missing(of _numeric_);

    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 7 replies
    • 434 views
    • 5 likes
    • 4 in conversation