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   Ada Ma <>
Subject   Re: st: Looping through entries in csv file
Date   Mon, 13 Sep 2010 15:12:35 +0100


A two step procedure can be as follows.

First put the whole list of of postcode sectors in your loop, or in a
global, and then stick the global into your loop.  This is an example
taken from my work.

global threeletters A18 A52 A55 A84 B32 B37 E25 E36 E49 E51 E63 G16 G19 G45 ///
G55 G65 G80 H22 H25 H28 J09 J13 J25 J43 J44 J45 J67 K58 L72 L91 L95 M11 M30 ///
M45 M47 M77 Q12 Q55 R02 R05 R14 R15 R24 R27 S13 S14 S15 S50 S51 S52 S53 T11 ///
T12 T43 T46 T81 T86 T90 V47 V49 W36 W87 W88 X29 X30 X31 X32 X33 X34 X35 X36 ///
X37 X38 X39 X40 X41 X42 X43 X44 X45 X46 X47 X48 X49 X50 X51

foreach v in $threeletters {
replace opertn_= "" if substr(opertn_,1,3)=="`v'"

Substitute the three letter codes for your postcode sectors and
replace the command within with a loop that loops through adds1-add6
to check for postcode sector.

Put all the extracted postcode into one variable, and merge it with
the postcode/county mapping file you have, then you will have one
county associated to each line of data.


On Mon, Sep 13, 2010 at 2:55 PM,  <> wrote:
> Hello Statalist,
> I have a bit of a data management problem, and I was hoping that you would
> be able to help me.
> I have a Stata data file containing about 60,000 names and addresses.  The
> addresses are held in 6 different variables, add1-add6, corresponding to
> different lines of the address.  The postcode can appear in any of the
> address variables.
> I also have a separate csv file that contains the postcode districts in
> Great Britain (the section of postcode before the space, e.g. for the
> postcode 'SK17 9JN' the postcode district is 'SK17'), and what county the
> district belongs to (for example, the county for SK17 is "East Midlands").
>  There are about 2,800 postcode districts in total.
> What I want to do is identify the county the individual lives in using the
> postcode of their address.  I thought that I might be able to do this as
> follows:
> For each line in the postcode csv file:
> 1. Identify if the postcode is present in any of the address variables
> using -strpos(s1,s2)-
> 2. If present, set the county of the individual to be the county
> corresponding to that postcode district from the csv file.
> However, I am struggling to find a way to loop through the rows of the csv
> file to enable me to search for each of the postcode districts in the
> addresses.
> I would greatly appreciate any help with this, as I am struggling to think
> of a way to do it.  Please let me know if anything needs clarifying.
> Many thanks,
> Gillian
> ------------------------------------------------------------------------
> 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
> expressed.
> 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:
> *
> *
> *

Ada Ma
Research Fellow
Health Economics Research Unit
University of Aberdeen, UK.
Tel: +44 (0) 1224 555189
Fax: +44 (0) 1224 550926

*   For searches and help try:

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