Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the logical error in codes of scenario A and B?

    Hello,
    I have a variable z_avg_days_taken_cb which consists of z scores. It has missing values too. I am using the following code to assign scores to each value by generating a new variable mks_avg_days_taken_cb. I have three scenarios for 3 different codes I am using:

    Scenario A:
    By runnning this code, all my values are coming as 0, even for the values which are less than 2.



    if z_avg_days_taken_cb > 2 {
    gen mks_avg_days_taken_cb = 0
    }
    else if z_avg_days_taken_cb > 1 & z_avg_days_taken_cb <= 2 {
    gen mks_avg_days_taken_cb = 1
    }
    else if z_avg_days_taken_cb > 0 & z_avg_days_taken_cb <= 1 {
    gen mks_avg_days_taken_cb = 2
    }
    else if z_avg_days_taken_cb > -1 & z_avg_days_taken_cb <= 0 {
    gen mks_avg_days_taken_cb = 3
    }
    else if z_avg_days_taken_cb > -2 & z_avg_days_taken_cb <= -1 {
    gen mks_avg_days_taken_cb = 4
    }
    else if z_avg_days_taken_cb < -2 {
    gen mks_avg_days_taken_cb = 5
    }
    else if z_avg_days_taken_cb == . {
    gen mks_avg_days_taken_cb = .
    }

    Scenario B:
    By running this code, all my new values are 2:


    if z_avg_days_taken_cb<-2 {
    gen mks_avg_days_taken_cb=5
    }
    else if z_avg_days_taken_cb>-2<=-1 {
    gen mks_avg_days_taken_cb= 4
    }
    else if z_avg_days_taken_cb>-1<=0 {
    gen mks_avg_days_taken_cb= 3
    }
    else if z_avg_days_taken_cb>0<=1 {
    gen mks_avg_days_taken_cb= 2
    }
    else if z_avg_days_taken_cb>1<=2 {
    gen mks_avg_days_taken_cb= 1
    }
    else if z_avg_days_taken_cb>2 {
    gen mks_avg_days_taken_cb= 0
    }
    else if z_avg_days_taken_cb=. {
    gen mks_avg_days_taken_cb= .
    }


    Scenario C: This code is giving the right answer

    gen mks_avg_days_taken_cb = .

    replace mks_avg_days_taken_cb = 5 if z_avg_days_taken_cb < -2 & !missing(z_avg_days_taken_cb)
    replace mks_avg_days_taken_cb = 4 if z_avg_days_taken_cb >= -2 & z_avg_days_taken_cb <= -1 & !missing(z_avg_days_taken_cb)
    replace mks_avg_days_taken_cb = 3 if z_avg_days_taken_cb > -1 & z_avg_days_taken_cb <= 0 & !missing(z_avg_days_taken_cb)
    replace mks_avg_days_taken_cb = 2 if z_avg_days_taken_cb > 0 & z_avg_days_taken_cb <= 1 & !missing(z_avg_days_taken_cb)
    replace mks_avg_days_taken_cb = 1 if z_avg_days_taken_cb > 1 & z_avg_days_taken_cb <= 2 & !missing(z_avg_days_taken_cb)
    replace mks_avg_days_taken_cb = 0 if z_avg_days_taken_cb > 2 & !missing(z_avg_days_taken_cb)


    Please let me know what are the logical errors in Scenario A and B.
    Thank you.
Working...
X