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: Renumbering variables in a loop of files


From   Sergiy Radyakin <serjradyakin@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Renumbering variables in a loop of files
Date   Fri, 17 Dec 2010 18:24:01 -0500

On Sat, Dec 18, 2010 at 12:15 AM, Florian Seliger
<florian.seliger@gmx.de> wrote:
> Dear Statalist,
>
> I have again a question concerning numbering  IDs in a foreach loop.
>
> I have multiple datafiles for that I have specified a foreach loop.
> Each dataset either contains a variable id_1, id_2, id_3, etc. or only a variable id without a number.
>
> My goal is to assign a number to all IDs in each file. The numbers should  reflect the number of the file, e.g.
>
> file no.                old     variable        --->    new variable
> 1                       id_1                    id_1
> 2                       id_2                    id_2
> 3                       id_3                    id_3
> 4                       id                              id_4
> 5                       id_4                    id_5
> 6                       id_5                    id_6
> 7                       id                              id_7
> 8                       id_6                    id_8
> ...                     ...                             ...     .
>


Florian, if you only have one variable id* per file the following
should suffice:

tempvar ID
rename id `ID'
rename `ID' id_`j'

here the second line delegates to Stata to unabbreviate what id is
(whether it is
id, id2, or id92) and rename it to some temporary name. Then it is renamed to a
properly numbered variable, where j is your file index. We need the temporary
variable to avoid renaming the variable into itself (Stata aborts on
"rename x x").
Alternatively, add capture, though then you might be missing other important
errors.



> I have specified a command such as the following:
>
> local i=1
> foreach file ... {
>
> use "`file'", clear
>
> forvalues j=1(1)230  {  //230 is the number of files
> capture confirm id_`j'
>
> if _rc==0 {
> sort id_`j'
>  capture rename id_`j' id_`i'
>                }
>                }
>
> else {
>  sort id
>  rename id id_`i'
> }
>
>
> local ++i
> save "`file'", replace
>                }
>
> ***end command***
>
> With this command, STATA tells me for the first file that id_1 is already defined and stops the loop although I have used a second  "capture" before "rename".

Command -confirm-, such as :confirm variable id_1

Example:
capture confirm variable id_`j'
if !_rc break, continue



Best, Sergiy Radyakin


> Do you have an idea how I have to rewrite the command?
>
> Thank you very much.
> Best wishes,
> Florian
>
>
> *
> *   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/
>

*
*   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