Announcement

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

  • Substring strvar into observations, not variables

    Dear Stata community,
    I have the following string variable r1etab that has multiple observations separated by a space as follows:

    r1etab Freq. Percent Cum.

    --- 31 9.87 9.87
    attention_rapide 15 4.78 14.65
    attention_rapide autre_a_preciser 1 0.32 14.97
    autre_a_preciser 9 2.87 17.83
    bon_service 11 3.50 21.34
    bon_service attention_rapide 16 5.10 26.43
    bon_service medicaments_facilement_di.. 4 1.27 27.71
    medicaments_facilement_disponibles 6 1.91 29.62
    medicaments_facilement_disponibles au.. 20 6.37 35.99
    proximite 79 25.16 61.15
    proximite attention_rapide 7 2.23 63.38
    proximite bon_service 10 3.18 66.56
    proximite bon_service attention_rapide 1 0.32 66.88
    proximite bon_service medicaments_fac.. 3 0.96 67.83
    proximite medicaments_facilement_disp.. 4 1.27 69.11
    proximite service_abordable 11 3.50 72.61
    proximite service_abordable bon_servi.. 1 0.32 72.93
    proximite service_abordable bon_servi.. 2 0.64 73.57
    service_abordable 7 2.23 75.80
    service_abordable attention_rapide 27 8.60 84.39
    service_abordable bon_service 42 13.38 97.77
    service_abordable bon_service attenti.. 3 0.96 98.73
    service_abordable bon_service medicam.. 1 0.32 99.04
    service_abordable medicaments_facilem.. 3 0.96 100.00


    When I tabsplit, the result is what I would like to have as a variable with observations
    r1etab Freq. Percent Cum.

    31 6.40 6.40
    attention_rapide 71 14.67 21.07
    autre_a_preciser 30 6.20 27.27
    bon_service 94 19.42 46.69
    medicaments_facilement_disponibles 43 8.88 55.58
    proximite 118 24.38 79.96
    service_abordable 97 20.04 100.00

    Total 484 100.00

    I would like to have a variable that splits these observations, not make them separate variables as I have seen in other posts about extracting string variables. Thank you!

  • #2
    Welcome to Statalist. For your future posts, please read the FAQ Advice and familiarize yourself on how to provide data examples using the dataex command. The following may be what you want.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str53 var1
    "attention_rapide"                      
    "attention_rapide autre_a_preciser"     
    "autre_a_preciser"                      
    "bon_service"                           
    "bon_service attention_rapide"          
    "medicaments_facilement_disponibles"    
    "proximite"                             
    "proximite attention_rapide"            
    "proximite bon_service"                 
    "proximite bon_service attention_rapide"
    "proximite service_abordable"           
    "service_abordable"                     
    "service_abordable attention_rapide"    
    "service_abordable bon_service"         
    end
    
    gen toexpand= wordcount(var1)
    gen obsno=_n
    expand toexpand
    bys obsno: gen wanted= word(var1, _n)
    Res.:

    Code:
    . sort wanted
    
    . l, sep(0)
    
         +------------------------------------------------------------------------------------------------+
         |                                   var1   toexpand   obsno                               wanted |
         |------------------------------------------------------------------------------------------------|
      1. |      attention_rapide autre_a_preciser          2       2                     attention_rapide |
      2. |             proximite attention_rapide          2       8                     attention_rapide |
      3. |           bon_service attention_rapide          2       5                     attention_rapide |
      4. |     service_abordable attention_rapide          2      13                     attention_rapide |
      5. | proximite bon_service attention_rapide          3      10                     attention_rapide |
      6. |                       attention_rapide          1       1                     attention_rapide |
      7. |                       autre_a_preciser          1       3                     autre_a_preciser |
      8. |      attention_rapide autre_a_preciser          2       2                     autre_a_preciser |
      9. |           bon_service attention_rapide          2       5                          bon_service |
     10. |                  proximite bon_service          2       9                          bon_service |
     11. |          service_abordable bon_service          2      14                          bon_service |
     12. | proximite bon_service attention_rapide          3      10                          bon_service |
     13. |                            bon_service          1       4                          bon_service |
     14. |     medicaments_facilement_disponibles          1       6   medicaments_facilement_disponibles |
     15. |                  proximite bon_service          2       9                            proximite |
     16. | proximite bon_service attention_rapide          3      10                            proximite |
     17. |            proximite service_abordable          2      11                            proximite |
     18. |             proximite attention_rapide          2       8                            proximite |
     19. |                              proximite          1       7                            proximite |
     20. |            proximite service_abordable          2      11                    service_abordable |
     21. |     service_abordable attention_rapide          2      13                    service_abordable |
     22. |          service_abordable bon_service          2      14                    service_abordable |
     23. |                      service_abordable          1      12                    service_abordable |
         +------------------------------------------------------------------------------------------------+

    Comment


    • #3
      Andrew Masau, thank you for the welcome and the guidance. It is much appreciated.
      I would like to do this for more than one variable with similar strings. How do I do more than just one _n?
      I will have var1 var2 var3....so how do I get an obsno1 obsno2 etc?
      Thanks

      Comment


      • #4
        I think you will have to reshape var1, var2,..., varN first then apply #2 as the number of elements across variables may not be the same for any given observation. Provide a data example as recommended in #2 for anything specific.

        Code:
        dataex var1 var2 var3 in 1/20

        Comment

        Working...
        X