Announcement

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

  • Adding a prefix to all values in a variable!

    Hi Statalisters! I am working on a dataset on school admissions that looks like this
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double id str7(schoolid p1 p2 p3 p4 p5 p6 p7)
    20160000015 "1412148" "1412148" "1413329" "1413240" "1413245" "1413215" "1411253" "1411219"
    20160000041 "."       "1821202" "1821210" "1821232" "."       "."       "."       "."      
    20160000053 "."       "1617224" "1617166" "1617186" "1617204" "1617220" "1411253" "1617192"
    20160000058 "."       "1821202" "1821146" "1821210" "1618189" "."       "."       "."      
    20160000088 "."       "1618249" "1821143" "1618232" "1514087" "1720147" "1514073" "1617192"
    20160000090 "1720158" "1720158" "1720136" "1514085" "1514086" "1515105" "1516110" "1516113"
    20160000097 "1105191" "1003262" "1002318" "1002294" "1001197" "1002306" "1003217" "1003225"
    20160000106 "1617204" "1617204" "1617186" "1617192" "1617181" "1617229" "1618253" "1515114"
    20160000107 "."       "1617192" "1617204" "."       "."       "."       "."       "."      
    20160000115 "1413330" "1412257" "1412152" "1413330" "1413225" "1413227" "1413329" "1412148"
    20160000122 "."       "1617229" "1411253" "1617182" "1617186" "1413283" "1413183" "1413215"
    20160000149 "1411219" "1411219" "."       "."       "."       "."       "."       "."      
    20160000150 "."       "1923255" "1720142" "1720145" "."       "."       "."       "."      
    20160000157 "."       "1413329" "1411253" "1617229" "."       "."       "."       "."      
    20160000228 "."       "1411183" "1411214" "1411201" "1207188" "1411199" "1207186" "1207181"
    20160000264 "."       "1617192" "1617229" "1617186" "1617182" "1617181" "1411253" "1514086"
    20160000270 "."       "1516116" "."       "."       "."       "."       "."       "."      
    20160000276 "1104284" "1105208" "1106190" "1104301" "1105233" "1106218" "1104310" "1104275"
    20160000283 "."       "1411253" "1412135" "1412141" "1412151" "1412156" "1413205" "1413215"
    20160000288 "1821173" "1821141" "1821232" "1821168" "1821145" "1821151" "1821152" "1821143"
    end
    schoolid is the code of the school that the child got admitted into (it also uniquely identifies all the schools) and p1-p7 are the preference variables (in my dataset I have p1-p178). I want to create a dummy variable for each school that takes a value 1 if the school is amongst the preferences and 0 otherwise.
    I tried to generate school dummies with this code (suggested on this forum for a case where school codes don't start with numbers)

    levelsof schoolid
    display `r(levels)'

    foreach v in `r(levels)' {
    gen `v' = 0
    quietly forval j = 1/178 {
    replace `v' = 1 if p`j' == "`v'"
    }
    }

    This code would work if the values of my schoolid and p* variables were not starting with numbers. Is there another way to generate dummies in this case? Or, is there a way to add a prefix to all the values in my schoolid and p* variables? If I can prefix an alphabet to every value, then the code above would create my dummies.

    Many thanks!
    Vijay
    Last edited by Vijay Kumar; 06 Aug 2016, 08:39.

  • #2
    You can add the prefix simply by putting some letter in front of the numeric macro when you generate the variable. However, you have to make sure that you leave out missing values of the schoolid variable. These minor changes to your code should do the trick:

    Code:
    levelsof schoolid if schoolid != "."
    display `r(levels)'
    
    foreach v in `r(levels)' {
        gen x`v' = 0
            quietly forval j = 1/178 {
                replace x`v' = 1 if p`j' == "`v'"
            }
    }
    I've only added the if-statement as well as the x as prefix. The result should look like this:

    Code:
    . list x1104284-x1821173 if inlist(1, x1104284, x1105191, x1411219, x1412148, x1413330, x1617204, x1720158, x1821173)
    
         +---------------------------------------------------------------------------------------+
         | x1104284   x1105191   x1411219   x1412148   x1413330   x1617204   x1720158   x1821173 |
         |---------------------------------------------------------------------------------------|
      1. |        0          0          1          1          0          0          0          0 |
      3. |        0          0          0          0          0          1          0          0 |
      6. |        0          0          0          0          0          0          1          0 |
      8. |        0          0          0          0          0          1          0          0 |
      9. |        0          0          0          0          0          1          0          0 |
         |---------------------------------------------------------------------------------------|
     10. |        0          0          0          1          1          0          0          0 |
     12. |        0          0          1          0          0          0          0          0 |
         +---------------------------------------------------------------------------------------+

    Comment


    • #3
      Dear Mathias, This works. Thank you so very much.

      Comment

      Working...
      X