Announcement

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

  • Will the 'For Each' Command work in this situation?

    Hi Everyone,

    I am analysing the data collected from NFHS-5 of India for a project.

    In the dataset, for each woman respondent, there are data entries (variables) for up to 20 children born to a given woman.
    The date of birth of children is coded in two different variables birthmonth1, birthmonth2, birthmonth3 and so on……. Up to 20.
    Similarly, the year of birth of the children is given as birthyear1, birthyear2, birthyear3 and so on…………….

    I want to generate the date of birth of all the children born to a woman.
    Right now I am using the following command

    gen dob1= mdy (birthmonth1, 15, birthyear1)

    However, I have to repeat the same command 20 times to generate the date of birth for all 20 children.

    Is there a way I can loop this may be by using ‘for each’ or any other command?

    I don’t want to repeat the command over and over again.


    Many Thanks

  • #2
    You're not new here, you should know (as you've sort of done before) that the best way for us to help you is by providing a data example as well as the code you've thus far tried

    Comment


    • #3
      Code:
      forval j = 1/20 { 
      
      gen dob`j' = mdy(birthmonth`j', 15, birthyear`j')
      
      }

      Comment


      • #4
        Originally posted by Nick Cox View Post
        Code:
        forval j = 1/20 {
        
        gen dob`j' = mdy(birthmonth`j', 15, birthyear`j')
        
        }
        Dr Nick,

        Thank you so so much. The code worked as I intended. However, I have one more question- how did you type 'j'? I have to copy-paste `j', is it '' (two-single inverted comma).

        Comment


        • #5
          The left single quote and right single quote are uchar(96) and uchar(39).

          Code:
          . di uchar(96)
          `
          
          . di uchar(39)
          '
          Naturally, I can't see your keyboard to tell you how to get them.

          Comment


          • #6
            On most Indian keyboards, the starting quote is the one on the key immediately to the left of the digit 1, and the ending quote is the one on the key immediately to the right of the semicolon.

            Comment


            • #7
              #6 matches my laptop.

              Comment

              Working...
              X