Search code examples
stata

% Change excluding NA coded variables (Stata)


I have two variables, where pr8 is the new period and pr7 is the old period, and I created the following code to create a percentage change variable:

gen pchange = (pr8-pr7)/pr7*100

However, I noticed that both variables: pr8 and pr7, contain values coded as (993) for "no answer" and (994) for "refused to answer", and I want to exclude such values from the percentage change calculation as they bias the descriptive statistics. Therefore, I would like to create a pchange variable that excludes the coded values above.

I fixed this with the code below, but not sure if it is the best way. I then ran the same code above.

replace pr7 = . if pr7 == 993
replace pr7 = . if pr7 == 994

replace pr8 = . if pr8 == 993
replace pr8 = . if pr8 == 994

Solution

  • Your best fix is to go -- ahead of the change calculation --

    foreach v in pr7 pr8 { 
        replace `v' = .a if `v' == 993 
        replace `v' = .b if `v' == 994 
    } 
    

    as otherwise there is no end to the work-around code that lies ahead. Define new value labels accordingly.