Announcement

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

  • Generate a row rather than Column

    Hello Everyone,

    May I ask if anyone has an idea if we can generate a row of data in stata? If I have a row, and I need to generate another row, with exact values, however, then rename one variable value in the new row.

    I have, for example:

    Code:
    geoid2           geodisplaylabel               median~e            popula~n          year
    603393500       UpperLake-Clearlake       44.7               11596             2009
    But I need to split these two words to be

    Code:
    geoid2           geodisplaylabel                median~e            popula~n          year
    603393500       Clearlake                         44.7               11596             2009
    603393500       UpperLake                       44.7               11596             2009
    Thank you in advance,
    Ali

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long geoid2 str19 geodisplaylabel float mediane int(populan year)
    603393500 "UpperLake-Clearlake" 44.7 11596 2009
    end
    
    expand 2
    split geodisplaylabel, gen(names) parse("-")
    sort geoid2
    forvalues i = 1/2 {
        by geoid2: replace geodisplaylabel = names`i' if _n == `i'
    }
    drop names*
    Stata is not a spreadsheet, and if you think of it as if it were, you will get into trouble sooner or leader. To avoid thinking about Stata as if it were a spreadsheet, it is best to avoid spreadsheet terminology. So in a Stata data set we refer to observations and variables, rather than rows and columns.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      That's appreciated very much. Thanks !

      Comment

      Working...
      X