Announcement

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

  • importing dataset with long variables names into stata through loop statements

    Dear Stata Users,

    I am trying to import dataset below into stata from csv test_data.csv but some variable names are too long and and are separated by '/' so they come out in the format v23, v24 v25, etc. Kindly I seek guidance on how I can write a loop statement in stata to import this dataset into stata without renaming the variables in the format v23, v24 and etc. I I would prefer to start ready from right and stops where the 1st '/' and pick that as the variable name. I believe this is possible but through loops. Kindly I would appreciate any guidance on how to fix this. see attached data format for guidance.

    Thank you.
    Collins

  • #2
    Perhaps the fastest approach is to edit the original CSV file in a text editor and edit the first line of the file so that each variable name is what you want it to be.

    Comment


    • #3
      Are the fields separated by "/" or is the "/" part of the names whiich are separated by comma? Does the the result of running the following make sense:
      Code:
      import delimited using test_data.csv , delim(",") clear
      If, so, the variables can be renamed:
      Code:
      import delimited using test_data.csv , delim(",") clear
      
      foreach v of varlist * {
      
          local lab : variable label `v'
          
          if ( ustrregexm("`lab'","[^/]+?$") ) {
          
              local end = ustrregexs(0)
          }
          
          rename `v' `end'
      }
      giving the following variable names
      Code:
       | position   name                      varlab                                                         |
        |-----------------------------------------------------------------------------------------------------|
        |        1   intstate                  hh_infogeneral/intstate                                        |
        |        2   intlocation               hh_infogeneral/intlocation                                     |
        |        3   calc_name                 hh_consent/calc_name                                           |
        |        4   intapproach               hh_consent/intapproach                                         |
        |        5   age_elig18over            hh_consent/g_tell/age_elig18over                               |
        |        6   timestamp_startB          hh_consent/g_tell/timestamp_startB                             |
        |        7   format_timestamp_startB   hh_consent/g_tell/format_timestamp_startB                      |
        |        8   timestamp_startB_hour     hh_consent/g_tell/timestamp_startB_hour                        |
        |        9   timestamp_startB_minute   hh_consent/g_tell/timestamp_startB_minute                      |
        |       10   timestamp_startB_totM     hh_consent/g_tell/timestamp_startB_totM                        |
        |       11   age_elig15to17            hh_consent/g_tell/age_elig15to17                               |
        |       12   calc_ack_en               hh_consent/g_tell/g_oldenough/calc_ack_en                      |
        |       13   calc_ack_es               hh_consent/g_tell/g_oldenough/calc_ack_es                      |
        |       14   intconsent                hh_consent/g_tell/g_oldenough/g_intconsent/intconsent          |
        |       15   int_signature             hh_consent/g_tell/g_oldenough/g_intconsent/int_signature       |
        |       16   calc_ack_en_adol          hh_consent/g_tell/g_oldenough/calc_ack_en_adol                 |
        |       17   calc_ack_es_adol          hh_consent/g_tell/g_oldenough/calc_ack_es_adol                 |
        |       18   intassent_adol            hh_consent/g_tell/g_oldenough/g_intassent/intassent_adol       |
        |       19   int_signature_adol        hh_consent/g_tell/g_oldenough/g_intassent/int_signature_adol   |
        |       20   intassent_parent          hh_consent/g_tell/g_oldenough/g_intassent/intassent_parent     |
        |       21   int_signature_parent      hh_consent/g_tell/g_oldenough/g_intassent/int_signature_parent |
        |       22   intconsent_no             hh_consent/g_tell/intconsent_no                                |
        +-----------------------------------------------------------------------------------------------------+
      Last edited by Bjarte Aagnes; 01 May 2019, 08:32.

      Comment

      Working...
      X