Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Looping through entries in csv file


From   Neil Shephard <nshephard@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Looping through entries in csv file
Date   Mon, 13 Sep 2010 14:44:57 +0000

On Mon, Sep 13, 2010 at 2:37 PM, Ada Ma <heu034@googlemail.com> wrote:
> Open up the postcode-county file using stata or excel.
>
> I suppose the postcode sectors are all in a column.  Select and copy it.
>
> Go into excel if you aren't already in excel.  Right click, choose
> "paste special".  Transpose the postcode.  Now the postcodes all in
> one row.  Copy it and paste into a do file.  Edit them so that they
> looks nice (this step is optional).

A more direct method would be to do this in Stata.  Something along
the lines of....

File with full Postcodes in is called postcodes.dta with the full
postcode held in the variable 'postcode'

use postcodes, clear
/* Get the first 3 characters of the postcode into a new variable */
gen sub_postcode = substr(postcode, 1, 3)
/* Store all of these unique values in a local macro using -levelsof- */
levelsof sub_postcode, local(postcode_districts)
di "`postcode_districts'"
/* Store all of these unique values in a global macro using -levelsof- */
levelsof sub_postcode, global(postcode_districts)
di "$postcode_districts"

(Use local or global macros depending on what you want to do with the
macrolist and how you're structuring/organising your do-files).

Neil

-- 
"One should as a rule, respect public opinion in so far as is
necessary to avoid starvation and to keep out of prison, but anything
that goes beyond this is voluntary submission to an unnecessary
tyranny, and is likely to interfere with happiness in all kinds of
ways..." - Bertrand Russell

Email - nshephard@gmail.com
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index