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

Subject   Re: st: Looping through entries in csv file
Date   Mon, 13 Sep 2010 16:44:12 +0100

Neil and Ada,

A huge thank you for your help so far, I really appreciate this.

Ada, unfortunately I think the number of postcode district (around 2,800) 
is greater than the number of rows in Excel, so I can't quite get my list 
that way.

Neil, thank you for pointing out the -levelsof- command.  I would really 
like the list as a global rather than a local, but this does not appear to 
be an option of -levelsof-.
I have tried the following code:

- - - - start code - - - -
levelsof district
global postcode_district = r(levels)

foreach l of global postcode_district {
        display "`l'"
- - - - end code - - - -

However, when I display the list at the end I only get 27 of the postcode 
districts displayed - the other codes don't appear to be in the list.
Also, is it possible to keep a global and use it when I -clear- the 
current dataset, and upload a different one?

Many thanks,


Neil Shephard <> 
Sent by:
13/09/2010 15:44
Please respond to


Re: st: Looping through entries in csv file

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:


This message contains privileged and confidential information intended
for the addressee(s) only. If this message was sent to you in error,
you must not disseminate, copy or take any action in reliance on it and
we request that you notify the sender immediately by return email.

Opinions expressed in this message and any attachments are not
necessarily those held by the Health and Safety Laboratory or any person
connected with the organisation, save those by whom the opinions were

Please note that any messages sent or received by the Health and Safety
Laboratory email system may be monitored and stored in an information
retrieval system.
Think before you print - do you really need to print this email?

Scanned by MailMarshal - Marshal's comprehensive email content security
solution. Download a free evaluation of MailMarshal at
*   For searches and help try:

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