Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

Re: st: Looping through entries in csv file

From   Neil Shephard <>
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 <> 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).


"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 -
Website -
Photos -

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index