Announcement

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

  • import delimited and skip some rows

    I am trying to import a csv file in which the first row has variable names (that I want to keep) and the rows I want to read start on line 4 (That is, I do not want to read rows 2 and 3).
    I have tried this

    . import delimited "myfile.csv", varn(1) delimiter(comma) rowrange(1 4: )

    but Stata does not like that row range syntax.
    Is there a way to specify a row range to read several rows, then skip several, and then read the rest of the rows?

  • #2
    All you have to do is:
    Code:
    import delimited using "myfile.csv", varnames(1) rowrange(4:) delimiter(comma)
    There is no requirement that the -varnames()- row be among the -rowrange()- rows.

    Comment


    • #3
      You don't need to specify 1 in rowrange() option since you've already specified it in the varn() option.

      Code:
      clear all
      version 18.0
      
      input strL name
      "Caren"
      "Natalia"
      "Maryna"
      "Jessica"
      "Valeria"
      "Helen"
      "Victoria"
      "Gabriela"
      end
      
      tempfile tmp
      outsheet using `"`tmp'"', delim(",")
      view `"`tmp'"'
      clear
      
      import delimited `"`tmp'"', varnames(1) delimiter(comma) rowrange(4:)
      
      list, clean
      Data file:
      Click image for larger version

Name:	names.png
Views:	1
Size:	7.9 KB
ID:	1735577


      Output:
      Code:
                 name  
        1.     Maryna  
        2.    Jessica  
        3.    Valeria  
        4.      Helen  
        5.   Victoria  
        6.   Gabriela

      Comment

      Working...
      X