Announcement

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

  • Replace first row with variable names

    Hi All,


    I have data that resembles the following:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 var2 int var5 float(var6 var7 var8)
    "Country Code" 1960 1961 1962 1963
    "ABC"            12   32   12    3
    "BCD"             3   21   32   12
    end

    In the above, the first row contains the variable names. So for instance, the first column contains country code, and the subsequent column contains data by different years. Unfortunately, the *.csv file I imported this from does not allow me to select the option to treat the first row as variable names. Is there any way to replace variable names with the first row?


    Many thanks,
    CS

  • #2
    Both import excel and import delimited will let you specify that the first row contains variable names. See here
    Note that Stata won't let you name a variable "1960" "1961" etc (they can't begin with a number), so I would go into the CSV file (or Excel) and manually rename them y1960, y1961, etc

    Code:
    import excel using my_file.xls, firstrow
    
    import delimited using my_file.csv, varnames(1)
    Last edited by David Benson; 13 Feb 2019, 18:04.

    Comment


    • #3
      Thanks a lot David Benson

      Comment


      • #4
        Dear Chinmay, Please ssc install nrow, and try
        Code:
        nrow
        ren var2 code
        ren _* yr*
        The result is
        Code:
        . list
        
             +------------------------------------------+
             | code   yr1960   yr1961   yr1962   yr1963 |
             |------------------------------------------|
          1. |  ABC       12       32       12        3 |
          2. |  BCD        3       21       32       12 |
             +------------------------------------------+
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Thanks a lot!!!

          Comment

          Working...
          X