Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Help with a loop


From   "Ada Ma" <[email protected]>
To   [email protected]
Subject   Re: st: Help with a loop
Date   Mon, 16 Jul 2007 15:15:10 +0100

Hi Guillermo,

I don't think you need a loop.  How about this:

gen string = string( Var1)+ string( Var2) + string( Var3) + string( Var4)
replace string = subinstr(string, ".","",.)
drop Var*
gen Var1 = substr(string,1,1)
gen Var2 = substr(string,2,1)
...

so on and so forth

This would work if all your numbers are single digits / all have
identical digits.  If they aren't consider generating string versions
of the variables and make them all the same lengths (put zeros in
front of small numbers), and then put them all together in one
variable, stripe out the spaces and missing's, and cut them apart
again.  Tedious but would work.

Hope this helps.
Ada




On 7/16/07, Guillermo Villa <[email protected]> wrote:
Dear statalisters,

I am working on a dataset that looks like this:

Var11 =09Var12=09Var13=09Var14=09Var15
1=092=093=09.=095
  =092=094=09.=09.
3=09.=094=09.=096
  =09.=09.=093=09.=09.

I would need to move all the values to the left, skipping the missing value=
s:

Var11 =09Var12=09Var13=09Var14=09Var15
1=092=093=095=09.
2=094=09.=09.=09.
3=094=096=09.=09.
3=09.=09.=09.=09.

I have tried the following:

foreach i of numlist 1/4 {
if Var1`i'=3D=3D. {
replace Var1`i'=3DVar1`i'+1
replace Var1`i'+1=3D=3D.
}
}

But I get this warning: + invalid name r(198), which refers to the
last replace command=85

Could you please help me? Do you think there is a more convenient way
of doing it?

Thanks.

Guillermo

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index