Announcement

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

  • Sum up # of observations and output to spreadsheet/csv

    Hello,

    I am trying to accumulate the number of observations for each dataset with a pattern of "consumers_YYYY_*". I was only able to "display" the number of observations. I wonder how we could "sum this up" and eventually make it as a csv for each year (i.e. two variables: one is # of observations for each year, the other is year). Thanks a lot!!



    global DATA "C:\Users\90596\data"

    cd "${DATA}"

    foreach y of num 2004/2018 {

    local files : dir "${DATA}" files "consumers_`y'*"

    cd "${DATA}"

    foreach file in `files' {
    use ids using `file', clear

    dis(_N)
    }
    }

  • #2
    The -file write- command should help:

    https://stats.idre.ucla.edu/stata/fa...nal-text-file/

    Comment


    • #3
      Thanks Prof. Feenberg!

      Comment


      • #4
        -file write- isn't even the most appropriate command for you. The better one is -postfile- because it creates a .dta file.

        https://www.stata.com/manuals/ppostfile.pdf

        I should have remembered that yesterday.


        Comment

        Working...
        X