Announcement

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

  • help with a double loop

    Hi All,

    I am having a brain freeze with this parallel double loop.

    I have a variable (shape) with nominal level data, and I want to (1) generate the same number of dummy variables as there are unique levels of shape, (2) populate each dummy variable with a 1 if the value in shape matches the level (in order) produced by levelsof, or 0 if not.

    With the data below, there are three categories, "round", "square", and "triangular". Thus, 3 dummy variables are produced. The first complete loop works as expected, that is, in shape[1] the value is "square" and it is assigned correctly to dummy variable 2 while the other two dummies get coded as 0.

    The problem starts at the next loop, where it appears that the first category ("round") is not assessed, and the code moves on to "square". My guess is that it has to do with the line - local ++cat - which moves forward one level at each loop rather than resetting back to "round"

    Any help would be appreciated!

    Ariel

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 shape
    "square"    
    "round"     
    "round"     
    "round"     
    "round"     
    "round"     
    "square"    
    "square"    
    "round"     
    "round"     
    "triangular"
    "triangular"
    end
    Code:
    levelsof shape, local(levs)
    local cnt : word count `levs'
    
    forvalues i = 1/`cnt' {
        gen dummy`i' = 0
    }
    
    local x = 1
    
    forvalues cat = 1/`cnt' {
        forvalues col = 1/`cnt'{
            replace dummy`col' = 1 if shape[`x'] == "`:word `cat' of `levs''" in `x'
            local ++cat
        }
        local x = `x' + 1
    }

  • #2
    Perhaps I don't follow what you're trying to achieve, but this code:

    Code:
    levelsof shape, local(levs)
    local cnt : word count `levs'
    
    
    forvalues i = 1/`cnt' {
        gen dummy`i' = (shape == "`:word `i' of `levs''")
    }
    produces:

    Code:
    . list, noobs sep(0)
    
      +---------------------------------------+
      |      shape   dummy1   dummy2   dummy3 |
      |---------------------------------------|
      |     square        0        1        0 |
      |      round        1        0        0 |
      |      round        1        0        0 |
      |      round        1        0        0 |
      |      round        1        0        0 |
      |      round        1        0        0 |
      |     square        0        1        0 |
      |     square        0        1        0 |
      |      round        1        0        0 |
      |      round        1        0        0 |
      | triangular        0        0        1 |
      | triangular        0        0        1 |
      +---------------------------------------+
    Are you looking for something else?

    Comment


    • #3
      I could swear I've seen a command for this before, possibly on SSC. Oh well. What about something like this:

      Code:
      levelsof shape, local(levs)
      foreach level in `levs'{
          gen d_`level' = shape == "`level'"
      }
      list, clean noobs
      Which produces this:

      Code:
               shape   d_round   d_square   d_tria~r  
              square         0          1          0  
               round         1          0          0  
               round         1          0          0  
               round         1          0          0  
               round         1          0          0  
               round         1          0          0  
              square         0          1          0  
              square         0          1          0  
               round         1          0          0  
               round         1          0          0  
          triangular         0          0          1  
          triangular         0          0          1
      Edit: crossed with #2.

      Comment


      • #4
        Thank you, Hemanshu and Daniel! I realized I was digging myself a deeper and deeper hole and you pulled me out!

        Have a great weekend!

        Ariel

        Comment


        • #5
          The same result as in #2 can also be achieved by simply doing:

          Code:
          xi i.shape, noomit
          rename _Ishape_* dummy*

          Comment


          • #6
            that's pretty fancy and efficient! Thank you, again!

            Ariel

            Comment


            • #7
              Hi Ariel,

              I think tabulate would also work here.

              Code:
              tabulate shape, generate(dummy) nofreq

              Comment


              • #8
                Thank you, Enrique! This reminds me of a game show many years ago called "name that tune". The idea was to name a tune in as few notes as possible. We're at the point in this code where I am not sure if it can be minimized any further!

                I did just realize that I'll have to convert all 1's to 2^-0.50. This will require me to keep track of the variables generated so that I can convert the 1's accordingly. I don't think there is a simple solution for this if I use -tab, gen- or -xi: i.shape-. I may have to resort to the first solution posted by Hemanshu so that I can track the names of the new variable generated.

                Comment

                Working...
                X