Announcement

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

  • Creating groups of one variable using nonmissing values in a second variable

    I have the following data:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1(var1 var2)
    "a" "x"
    "f" "" 
    "e" "" 
    "g" "" 
    "d" "" 
    "h" "" 
    "b" "x"
    "s" "" 
    "e" "" 
    "y" "" 
    "f" "" 
    "h" "x"
    "h" "" 
    "f" "" 
    "d" "x"
    "e" "" 
    "r" "" 
    "y" "" 
    "e" "" 
    "e" "" 
    end
    My goal is to create a third, numeric variable which groups var1 based on the position of "x" in var 2. My finished product should be:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1(var1 var2) float var3
    "a" "x" 1
    "f" ""  1
    "e" ""  1
    "g" ""  1
    "d" ""  1
    "h" ""  1
    "b" "x" 2
    "s" ""  2
    "e" ""  2
    "y" ""  2
    "f" ""  2
    "h" "x" 3
    "h" ""  3
    "f" ""  3
    "d" "x" 4
    "e" ""  4
    "r" ""  4
    "y" ""  4
    "e" ""  4
    "e" ""  4
    end

    Thanks!

  • #2

    Code:
    gen var3 = sum(var2 != "")

    Comment

    Working...
    X