Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Loop to replace values of a variable


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: Loop to replace values of a variable
Date   Wed, 18 Dec 2013 09:34:09 -0500

A solution that doesn't require knowing the value of x[1] in advance:

 sort x
 replace x=x[_n-1]+0.1 if _n>1

hth,
Jeph


On 12/18/2013 9:04 AM, emanuele mazzini wrote:
Yes, the way Marteen suggested worked greatly.
Thank you!!

2013/12/18 Fabrizio Colella - 1348010 <[email protected]>:
you generated x as Marteen suggested, now you just have to subtract:

gen x = _n*.1
gen y = -2.6+x

Fabrizio


----- Messaggio originale -----
Da: "Nick Cox" <[email protected]>
A: [email protected]
Sent: Mercoledì, 18 dicembre 2013 14:49:34
Oggetto: Re: st: Loop to replace values of a variable

For -2.5(0.1)2.1 or -2.5(0.1)2.5, you can still divide integers by 10.
No loop needed as _n always gives you integers in sequence.


Nick
[email protected]


On 18 December 2013 13:44, emanuele mazzini <[email protected]> wrote:
Oh, yes, I see. Thank you very much for your answers, but I may have
oversimplified, since what you said would fit in this case, as x is
exactly = _n/10, but my problem is slightly more complex.

What if the values I want to replace go from -2.5 to 2.15, still with
a 0.1 interval? In this case I'd still need a loop, right?

2013/12/18 Maarten Buis <[email protected]>:
no need to loop:

gen x = _n*.1

Hope this helps,
Maarten


On Wed, Dec 18, 2013 at 2:33 PM, emanuele mazzini
<[email protected]> wrote:
Dear all statalist users,

I would like to program a (relatively) simple loop to replace the
values of a variable I created, but I cannot  figure it out how to
write the syntax.

The structure of my data is very simple:
I have 300 observations and a variable, say x, that in the first cell
(i.e. for n=1) is equal to 0.1, while it is missing for all other 299
cells. Data look like:

n     x
1    0.1
2     .
3     .
4     .
.
.
.
.
.
299  .
300  .

I would like the loop to replace the missing values of my variable by
going from cell 2 to cell 300, and to have a replace that would be at
a 0.1 intervals, so as to obtain data that look like:

n     x
1    0.1
2    0.2
3    0.3
4    0.4
.
.
.
.
.
299  29.9
300  30

I can obviously do it by hand, but a loop would be naturally much more
efficient!
Does anyone know how to make it?

Thank you in advance.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/



--
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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