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]

st: Replacing grouped string values with longest string value in the group

From   "McDermaid, Cameron" <>
To   <>
Subject   st: Replacing grouped string values with longest string value in the group
Date   Thu, 19 Aug 2010 13:27:50 -0400

Stata 11 SE and a new STATA user

I want to replace string values in a group to the longest string value
from the same variable within the group.

I have a dataset with a number of symptoms that come from different
hospitals. Because of differences in coding practices and errors, there
can be differences in values (e.g. spelling).  I will not know how many
symptom values will be present in a given data set and the symptom
values are always strings. I've grouped the variables by soundex code
and want to use the longest Symptom value to replace the shorter ones in
the group. 

The intent is to write an ado file that anyone can run with minimum
interfacing to generate frequencies of Symptoms after they've been
processed as above.

I've processed the data such that the longest value by slike(soundex)
group is flagged:

Symptom	                	|slike	|slength	|slongest
BLURREDVISION			|B463		|13		|1
CONVULSIONS				|C514		|11
DIZZY					|D200		|5
DIZZY/VERTIGO			|D252		|13		|0


My approach has been:

.levelsof slike, local(sound)   
.foreach 1 of local sound {      
.if slike=="`1'" & slongest==1 {    
.local name=Symptom		 
.replace Symptom="`name'" if slike=="`1'" & slongest==0   

What appears to be happening is that the local macro `name' does not get
re-assigned with the Symptom value corresponding to the next level of
slike in the foreach loop.  All values of Symptom with an slongest value
of 0 are recoded to the first symptom: ALTEREDLEVELOFCONSCIOUSNESS

I suspect my approach is close, but the loop is failing to function as I
expected.  Any suggestions or alternate approaches would be greatly

- cam

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

You can't fix by analysis what you bungled by design -  Light, Singer,
and Willett

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'esent courriel a 'et'e exp'edi'e par le syst`eme 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'evu est interdite. 
Si vous avez recu le message par erreur, veuillez m'en aviser par 
t'el'ephone (au num'ero pr'ecit'e) ou par courriel, puis supprimer 
sans d'elai 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