Announcement

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

  • I want to divide the numlist in two groups.

    Hi,


    I have a loop with 50 values "foreach j of numlist 1/50 {", and I want to divide the numlist in two groups.

    In the first group I want the next values N1=1,...,25
    In my second group I want the other values N2=26,...50

    Because I want this to generate this vars:

    PRI_N1_1
    PRI_N1_...
    PRI_N1_...
    PRI_N1_...
    PRI_N1_25
    PRI_N2_26
    PRI_N2_...
    PRI_N2_...
    PRI_N2_...
    PRI_N2_50

    Would appreciate any help!

  • #2
    You need two loops, for example:
    Code:
    ​​​​​​​clear
    set obs 1
    forvalues i = 1/2 {
        forvalues j = 1/25 {
            gen PRI_N`i'_`=`j'+25*(`i'-1)' = .
        }
    }

    Comment


    • #3
      Originally posted by Braulio Santos View Post
      You need two loops, for example:
      Code:
      clear
      set obs 1
      forvalues i = 1/2 {
      forvalues j = 1/25 {
      gen PRI_N`i'_`=`j'+25*(`i'-1)' = .
      }
      }
      Thanks for your answer but with " (`i'-1)' " the first var wouldn't be "PRI_N1_0"?

      I tried to reply your code and the answer was:

      variable PRI_1_0 already defined
      r(110);

      Comment


      • #4
        No, the first variable would be PRI_N1_1.
        `j'+25*(`i'-1) = 1 + 25*(1-1) = 1 + (25*0) = 1

        When I run the code:
        Code:
        clear
        set obs 1
        forvalues i = 1/2 {
            forvalues j = 1/25 {
                gen PRI_N`i'_`=`j'+25*(`i'-1)' = .
            }
        }
        unab varlist: *
        di "`varlist'"
        I obtain
        Code:
        PRI_N1_1 PRI_N1_2 PRI_N1_3 PRI_N1_4 PRI_N1_5 PRI_N1_6 PRI_N1_7 PRI_N1_8 PRI_N1_9 PRI_N1_10 PRI_N1_11 PRI_N1_12 PRI_N1_13 PRI_N1_14 PRI_N1_15 PRI_ > N1_16 PRI_N1_17 PRI_N1_18 PRI_N1_19 PRI_N1_20 PRI_N1_21 PRI_N1_22 PRI_N1_23 PRI_N1_24 PRI_N1_25 PRI_N2_26 PRI_N2_27 PRI_N2_28 PRI_N2_29 PRI_N2_ > 30 PRI_N2_31 PRI_N2_32 PRI_N2_33 PRI_N2_34 PRI_N2_35 PRI_N2_36 PRI_N2_37 PRI_N2_38 PRI_N2_39 PRI_N2_40 PRI_N2_41 PRI_N2_42 PRI_N2_43 PRI_N2_44  > PRI_N2_45 PRI_N2_46 PRI_N2_47 PRI_N2_48 PRI_N2_49 PRI_N2_50
        It's strange. I have no idea how you got the variable "PRI_1_0".
        Last edited by Braulio Santos; 12 Mar 2019, 19:31.

        Comment


        • #5
          The only thing that comes to mind is that there is a copy-paste problem. Sometimes, when you copy code, Stata doesn't read the local macro delimiters (" ` " and " ' ") correctly. Try typing the code.
          Last edited by Braulio Santos; 12 Mar 2019, 19:27.

          Comment


          • #6
            Another way to do it is pedestrian but likely to be easier to follow.

            Code:
            forval j = 1/25 { 
                 gen PRI_N1_`j' = .
            } 
            
            forval j = 26/50 { 
                 gen PRI_N2_`j' = .           
             }
            By the time I have read the nested loops and worked out what they do, I've lost any advantage in writing them that way.

            Comment


            • #7
              Originally posted by Braulio Santos View Post
              No, the first variable would be PRI_N1_1.
              `j'+25*(`i'-1) = 1 + 25*(1-1) = 1 + (25*0) = 1

              When I run the code:
              Code:
              clear
              set obs 1
              forvalues i = 1/2 {
              forvalues j = 1/25 {
              gen PRI_N`i'_`=`j'+25*(`i'-1)' = .
              }
              }
              unab varlist: *
              di "`varlist'"
              I obtain
              Code:
              PRI_N1_1 PRI_N1_2 PRI_N1_3 PRI_N1_4 PRI_N1_5 PRI_N1_6 PRI_N1_7 PRI_N1_8 PRI_N1_9 PRI_N1_10 PRI_N1_11 PRI_N1_12 PRI_N1_13 PRI_N1_14 PRI_N1_15 PRI_ > N1_16 PRI_N1_17 PRI_N1_18 PRI_N1_19 PRI_N1_20 PRI_N1_21 PRI_N1_22 PRI_N1_23 PRI_N1_24 PRI_N1_25 PRI_N2_26 PRI_N2_27 PRI_N2_28 PRI_N2_29 PRI_N2_ > 30 PRI_N2_31 PRI_N2_32 PRI_N2_33 PRI_N2_34 PRI_N2_35 PRI_N2_36 PRI_N2_37 PRI_N2_38 PRI_N2_39 PRI_N2_40 PRI_N2_41 PRI_N2_42 PRI_N2_43 PRI_N2_44 > PRI_N2_45 PRI_N2_46 PRI_N2_47 PRI_N2_48 PRI_N2_49 PRI_N2_50
              It's strange. I have no idea how you got the variable "PRI_1_0".
              Thank you! It has been a great help

              Comment

              Working...
              X