Announcement

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

  • spmat import csv files

    Hi,

    I am trying to import an adjacency matrix (2927x2927) in Stata which I created in R based on a shp file. However, when I arrive to the step:

    "spmat import SPWMat using Sus_2017_FINAL_2.csv, replace normalize(row)"

    I get an error message:

    "Error in line 1 of file Sus_2017_FINAL_2.csv"

    My csv file has no "alphanumeric" elements. In the first row, there is only a number in the top left cell stating the total number of observations. In the first column, there are codes for my observations starting from the second row.

    My problem seems to be very similar with the problems here:

    https://www.researchgate.net/post/Im...rices_in_Stata

    and here:

    http://www.statalist.org/forums/forum/general-stata-discussion/general/295364-spmat-import-txt-files

    However, in neither forum a clear solution was provided.

    Basically, I would like to export this csv file in Stata to a spmat file so that I could apply spatial analysis using this matrix. Can you please help me with this? I will appreciate your help a lot.

    Michal

    P.S. Here you can download my csv file:

    http://www.mediafire.com/file/7q7ot4...2017_FINAL.csv
    Last edited by Michal Kotrc; 24 Mar 2017, 07:11.

  • #2
    Using the downloadable Sus_2017_FINAL.csv file:

    Code:
    clear all
    import delimited Sus_2017_FINAL.csv
    drop in 1
    count
    local N = r(N)
    insobs 1, before(1)
    replace v1 = `N' in 1
    count
    local N = r(N)
    forvalues i=2/`N' {
        qui replace v`i' = 0 in `i'
    }
    export delimited Sus2017, replace novarnames delimit(" ")
    spmat import SPWMat using Sus2017.csv, replace normalize(row)
    spmat su SPWMat

    Comment


    • #3
      Thanks a lot for your code, Rafal. It works perfectly! For the other guys who read this thread: actually the file I posted a link to is not changed in such a way (sorry for that) that "in the first row, there is only a number in the top left cell stating the total number of observations and in the first column, there are codes for my observations starting from the second row" as I posted initially. Therefore the code Rafal posted includes also commands to change the data to the quoted form above.

      Comment


      • #4
        i also have a this question, could you help me why my matrix is not correct , my matrix is https://www.dropbox.com/s/b1tidh569d...rices.dta?dl=0

        Comment


        • #5
          Hi,
          I have tried to run the commands provided here (as well as the ones provided by Emad Shehata in https://www.statalist.org/forums/for...port-txt-files) and I am not importing my matrix successfully. I still get the message "Error in line one [...]"
          Could anyone please help me? My matrix can be found in: https://www.dropbox.com/s/dandf0i13n...atrix.dta?dl=0

          Comment

          Working...
          X