Announcement

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

  • replace for all other rows

    Dear all,

    I would like to change all the rows to 0 if år_nr==2 & årBelopp_SJP==0 by lopnr_personid.
    and I would like to change all the rows to 1 if år_nr==2 & årBelopp_SJP>0 by lopnr_personid. It should be simple but the first statement doesn't work as I want according to my coding. Any suggestion how to go about it?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double lopnr_personid float(år_nr årBelopp_SJP sjukpenning_åretinnan)
    6 -3 0 1
    6 -3 0 1
    6 -3 0 1
    6 -3 0 1
    6 -3 0 1
    6 -3 0 1
    6 -3 0 1
    6 -3 0 1
    6 -3 0 1
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -2 0 0
    6 -1 0 1
    6 -1 0 1
    6 -1 0 1
    6 -1 0 1
    end

    Code:
    bysort lopnr_personid: gen sjukpenning_åretinnan= 1
    bysort lopnr_personid: replace sjukpenning_åretinnan= . if årBelopp_SJP>0 & år_nr==-2
    bysort lopnr_personid: replace sjukpenning_åretinnan = 0 if årBelopp_SJP == 0 & år_nr==-2
    
    bysort lopnr_personid: replace sjukpenning_åretinnan = 0 if sjukpenning_åretinnan[_n-1]==1
    bysort lopnr_personid: replace sjukpenning_åretinnan = sjukpenning_åretinnan[_n-1] if sjukpenning_åretinnan==.

  • #2
    It probably doesn't work because your text description doesn't match your code.

    What should happen when årBelopp_SJP is <0 or missing?

    Do you want a constant value across all observations for each person?

    Comment


    • #3
      You could adapt this template (warning code is not tested) which aims to replace all values for a given person with a constant, based on whether any records meet a condition. You will need to determine what happens for "ties" where multiple conditions may be true within the same person, if that's possible.

      Code:
      gen new = . // it's a good idea to not change raw data
      bysort person: replace new = 0 if x==2 & inrange(y, 0, .)
      bysort person: replace new = 1 if x==2 & y<0
      // At this point, specific rows have been changed in new, and you can inspect the original data values to see how they might change
      
      // Now replace those values that would remain unchanged
      replace new = original if missing(new)

      Comment


      • #4
        What should happen when årBelopp_SJP is <0 or missing?
        The variable årBelopp_SJP dont include årBelopp_SJP <0 or missing does not exist.


        Do you want a constant value across all observations for each person?
        yes, I would like to do the following:

        1) identify for the variable årBelopp_SJP when årBelopp_SJP ==0 for år_nr==-2 and replace 0 for all år_nr which goes from -4 to 4.

        2) identify for the variable årBelopp_SJP when årBelopp_SJP >0 for år_nr==-2 and replace1 for all år_nr which goes from -4 to 4.

        Comment


        • #5
          Thanks but the coding doesn't seem to be doing what I really want to........any other suggestions?

          Comment

          Working...
          X