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: Replacing grouped string values with longest string value in the group

From   "McDermaid, Cameron" <>
To   <>
Subject   Re: st: Replacing grouped string values with longest string value in the group
Date   Fri, 20 Aug 2010 08:25:23 -0400

Thanks to Nick and Maarten for the solution to my problem and thanks to Nick for the learning references!

- cam

Cameron McDermaid MHSc
Ottawa Public Health
100 Constellation Cr, Ottawa ON K2G 6J8
vox: 613.580.6744x15321
fax: 613.580.9601

All models are wrong, some models are useful - George Box 


Date: Thu, 19 Aug 2010 18:36:16 +0100
From: Nick Cox <>
Subject: st: RE: Replacing grouped string values with longest string value in the group

No loop is needed. 

gen length = length(Symptom)
bysort slike (length) : replace symptom = symptom[_N] 

For a tutorial on -by:- see 

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step by: step
        Q1/02   SJ 2(1):86--102                                  (no commands)
        explains the use of the by varlist : construct to tackle
        a variety of problems with group structure, ranging from
        simple calculations for each of several groups to more
        advanced manipulations that use the built-in _n and _N

Your loop is failing because of a confusion between -if- command and -if- qualifier. -if- as you are using it looks only at the first observation and in no will sense loop as you would wish. 


FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if qualifier
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J. Wernow
        6/00    I have an if or while command in my program that
                only seems to evaluate the first observation,
                what's going on?

In addition, your -foreach- loop is just a loop over one value. 

But there is no need to fix your loop as the code above can be used. 


This e-mail originates from the City of Ottawa e-mail system. Any 
distribution, use or copying of this e-mail or the information it 
contains by other than the intended recipient(s) is unauthorized. 
If you are not the intended recipient, please notify me at the 
telephone number shown above or by return e-mail and delete 
this communication and any copy immediately. Thank you.

Le présent courriel a été expédié par le système de courriels de 
la Ville d'Ottawa. Toute distribution, utilisation ou 
reproduction du courriel ou des renseignements qui s'y trouvent 
par une personne autre que son destinataire prévu est interdite. 
Si vous avez reçu le message par erreur, veuillez m'en aviser par 
téléphone (au numéro précité) ou par courriel, puis supprimer 
sans délai la version originale de la communication ainsi que 
toutes ses copies. Je vous remercie de votre collaboration.

*   For searches and help try:

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