Announcement

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

  • Content only in every 25th case

    I want to create a variable which contains the value of 1 in every 25th case, all other cases should hava a missing value. The dataset has in total 1000 cases.
    How can I do this? Any suggestions? Thanks for your help!

  • #2
    Sue:
    welcome to the list.
    You may want to try:
    Code:
    set obs 25
    forvalues i = 1(1)40 {
    generate x`i' =1 in 25
    replace x`i' =0 if x`i'==.
    }
    stack x1-x40, into(A)
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      No loop is needed

      Code:
      gen foobar = mod(_n, 25) == 0
      See also http://www.stata-journal.com/sjpdf.h...iclenum=pr0031

      Comment


      • #4
        cond(mod(_n , 25) == 0, 1, .)

        Comment


        • #5
          Thanks, Nick.
          Kind regards,
          Carlo
          (Stata 18.0 SE)

          Comment


          • #6
            Note further that if what is desired is to flag (select, do something special) to every 25th observation that no new variable is necessary as

            Code:
            if mod(_n, 25) == 0
            or

            Code:
            if !mod(_n, 25)
            will do it.

            (These can be put into words as flagging if the observation number is divisible by 25.)

            If you do prefer an indicator variable then a 1 or 0 variable is usually easier to work with than a 1 or missing variable.

            Comment


            • #7
              Thank you for your help and your solutions!

              Comment


              • #8
                Nick's advice is the ultimate one, definitely.

                Just for the sake of amusing myself, keeping up with the learning process and presenting an alternative, albeit less elegant, I gather Sue could also try this:

                (assuming she already has "oldvar" as a sequence)

                Code:
                . gen newvar = 1 if oldvar == 25
                Below, just a description of the steps, assuming she needs to create the data set from scratch:

                The first 2 lines are just to create a data set with 1000 observations and a sequence of number from 1 to 25

                Code:
                . set obs 1000
                number of observations (_N) was 0, now 1,000
                
                . egen float myseq = seq(), from(1) to(25) block(1)
                
                */ To get 1 for every time the sequence is equal to 25
                . gen tag25 = 1 if myseq ==25
                (960 missing values generated)
                
                */ Shall you have sequences longer than 25, say, the double (50):
                . egen float myseq2 = seq(), from(1) to(50) block(1)
                
                */ you will still get the same result, as wished; below, just the same, with a different name for the variable
                . gen tag25_b = 1 if myseq2 ==25
                (980 missing values generated)
                
                . count if tag25 !=.
                  40
                
                . count if tag25_b !=.
                  20
                Best regards,

                Marcos

                Comment

                Working...
                X