Announcement

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

  • Specifying a 'fill in the blank' variable in a loop

    Hello,

    I'm sure this is a function but I'm having trouble finding it --
    I'm trying to make a loop that behaves differently for each observation depending on the value for one of the variables.

    I have this command right now,

    Code:
     foreach f in Allergy_Envir Allergy_Skin Allergy_Other Asthma Psoriasis Thyroiditis T1Diabetes Uveitis DownSyndrome{
          egen `f'_sensitivity = rowtotal(ind_`f'_w1 - ind_`f'_w5) if start_of_symptoms == 5
    My variable, start_of_symptoms, ranges from 1 to 21. I want to create a loop that can go through all the values of start_of_symptoms and add up the ind_`f'_w* variables accordingly.


    I was trying to do this,

    Code:
     foreach f in Allergy_Envir Allergy_Skin Allergy_Other Asthma Psoriasis Thyroiditis T1Diabetes Uveitis DownSyndrome{
           foreach w in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21{
                  replace `f'_sensitivity = rowtotal(ind_`f'_w1 - ind_`f'_w`w') if start_of_symptoms == `w'
    But I quickly realized you can't use the replace command like that.

    Is there an easy way to set this up?


    Thank you,
    Jon

  • #2
    Probably better to do that in long form:

    Code:
    clear
    input ind_a_w1 ind_a_w2 ind_a_w3 ind_a_w4 ind_a_w5 ind_b_w1 ind_b_w2 ind_b_w3 ind_b_w4 ind_b_w5 start_of_symptoms
    1 1 1 1 1 2 2 2 2 2 1
    1 1 1 1 1 2 2 2 2 2 2
    1 1 1 1 1 2 2 2 2 2 3
    1 1 1 1 1 2 2 2 2 2 4
    1 1 1 1 1 2 2 2 2 2 5
    end
    
    gen id = _n
    
    reshape long ind_a_w ind_b_w, i(id) j(response)
    
    foreach x in ind_a_w ind_b_w{
        replace `x' = . if response > start_of_symptoms
    }
    
    collapse (sum) ind*, by(id)
    
    rename ind_*_w *_sensitivity
    And then save it and merge it back to the main file.
    Last edited by Ken Chui; 30 Aug 2022, 16:14.

    Comment


    • #3
      Hi Ken,

      Thank you for your response!

      Is there a way to do the "input" command in an automatic way? I have 140k observations in total that I have to do this for. Also, I have around 30 variables that i need to include in this (I gave an example with one of the set of variables that I'm working with.)
      It seems like my stuff is on too big of a scale for this command?

      I'm also having a little trouble understanding what j(response) and 'if response > start_of_symptoms' mean conceptually? I don't really know much about long/wide, and I read up on it a bit. I don't understand how if 'response' is in 'long' format and 'start_of_symptoms' is in 'wide,' how one can be > than the other?

      Thank you,
      Jon
      Last edited by Johnathan Athanasius; 01 Sep 2022, 19:14. Reason: Typo!

      Comment


      • #4
        This might do it without the need to reshape data:

        Code:
        foreach f in Allergy_Envir Allergy_Skin Allergy_Other Asthma Psoriasis Thyroiditis T1Diabetes Uveitis DownSyndrome{
            forval w = 1/21 {
                  egen `f'_sensitivity_`w' = rowtotal(ind_`f'_w1 - ind_`f'_w`w') if start_of_symptoms == `w'
            }
            egen `f'_sensitivity = rowtotal(`f'_sensitivity_*)
            drop `f'_sensitivity_*
        }
        Also, the input command was used in #2 to create a toy data example. You don't need it in your own work. However, as suggested in Statalist FAQ #12, in the future please do include a data extract with your questions so it is easier for others to troubleshoot. You can do this using the -dataex- command.
        Last edited by Hemanshu Kumar; 01 Sep 2022, 22:49.

        Comment


        • #5
          Originally posted by Hemanshu Kumar View Post
          This might do it without the need to reshape data.
          This worked perfectly! Thank you very much for your help! I didn't know about the command forval until now. As for the input command, that makes more sense now, I understand!

          Thanks,
          Jon

          Comment

          Working...
          X