Announcement

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

  • do i need to use foreach to create this variable?

    think i need to use the foreach command to create a variable but i am not sure. Can you help me with this syntax?

    I want to create a variable that equals 1 if ERDC always equals 0 for a given AdultID. here are a few lines of my observations. In this example ERDC is always "0" for AdultID AA45184F, but not for AdultID AA48568G. So the variable i want to create would be Always0ERDC = 1 for all AdultID AA45184F observations, and 0 for all Adult AA48568G observations.

    I'm thinking i may need to use the foreach syntax to create this, but i don't quite fully understand the syntax. I'm hoping someone here can help me write it or let me know if i am going about this totally wrong and need to use a different method.

    AdultID BeneMonth ChildID FY Metro period ERDC ERDC1stmo
    AA45184F 201910 EE101K8B 2020 1 1 0 0
    AA45184F 201911 EE101K8B 2020 1 2 0 0
    AA45184F 201912 EE101K8B 2020 1 3 0 0
    AA45184F 202001 EE101K8B 2020 1 4 0 0
    AA45184F 202002 EE101K8B 2020 1 5 0 0
    AA45184F 202003 EE101K8B 2020 1 6 0 0
    AA45184F 202004 EE101K8B 2020 1 7 0 0
    AA45184F 202005 EE101K8B 2020 1 8 0 0
    AA45184F 202006 EE101K8B 2020 1 9 0 0
    AA45184F 202007 EE101K8B 2020 1 10 0 0
    AA48568G 201911 AF301J0K 2020 1 2 1 1
    AA48568G 201912 AF301J0K 2020 1 3 1 1
    AA48568G 202001 AF301J0K 2020 1 4 1 1
    AA48568G 202002 AF301J0K 2020 1 5 1 1
    AA48568G 202003 AF301J0K 2020 1 6 1 1
    AA500M4B 201910 EC301J3T 2020 1 1 1 1
    AA500M4B 201911 EC301J3T 2020 1 2 1 1
    AA500M4B 201912 EC301J3T 2020 1 3 1 1
    AA500W9N 202012 WA401L1I 2021 0 15 1 1


  • #2
    No loop is needed.

    Code:
    bys AdultID (ERDC): gen wanted= ERDC[1]==ERDC[_N] & !ERDC
    See https://www.stata.com/support/faqs/d...ions-in-group/

    Comment


    • #3
      Thank you so much Andrew! so much easier than i was making it. Can you help me understand this code? does ERDC[1] ==ERDC[_N] mean that the first observation for ERDC is the same for all ERDC observations? If so, why is & !ERDC needed?

      Also, how would i write this if i wanted to get a variable for an AdultID that is sometimes ERDC=0, but not always?
      Last edited by Laur Hous; 13 Apr 2023, 11:58.

      Comment

      Working...
      X