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: foreach and forvalues combined


From   "Fabian Schönenberger" <sch.f@gmx.ch>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: foreach and forvalues combined
Date   Wed, 04 Jul 2012 17:41:44 +0200

Here is a short extract:
conm = Company Name, timeyear=year of accounting data, size=Market Cap, bm=BM-Ratio, sizep5= Size portfolio (here 1, meaning that these companies belong to the smallest ones), bmp5= BM-ratio (variies between 2 to 5).

conm	        timeyear	size	bm	       sizep5	bmp5
WATSCO INC	1975	        2.58	1.290924	1	3
FLAGG IND	1975	        2.67	.9136011	1	2
MASTERS INC	1975	        2.68	5.718543	1	5
RAVEN IND INC	1975	        2.71	1.720374	1	4
KROY INC	1975	        2.74	1.834639	1	4

My goal is now to create a new variable sizebmp5 which combines sizep5 and bmp5. And I just realized that of course I can simply combine these two variables to one, because of the breakpoints! These breakpoints do not change if the sample is restricted to sizep5==1 and so on. So, thank you Nick for making me rethink about my "problem" and sorry for the time I have stolen you.

Fabian

-------- Original-Nachricht --------
> Datum: Wed, 4 Jul 2012 16:06:00 +0100
> Von: Nick Cox <njcoxstata@gmail.com>
> An: statalist@hsphsun2.harvard.edu
> Betreff: Re: st: foreach and forvalues combined

> Sorry; I read this twice and didn't understand it, so I am bailing out
> here. You understand what you want, but this is a lot of words for me
> to understand all at once, especially as most of the jargon makes no
> sense to me.
> 
> I asked for a simple realistic example of what the data look like now
> and what you want to get. That means the sort of thing you would get
> from a -list-.
> 
> Of course, if somebody else understands this and can advise you that
> would be great.
> 
> Nick
> 
> On Wed, Jul 4, 2012 at 3:56 PM, "Fabian Schönenberger" <sch.f@gmx.ch>
> wrote:
> > Many thanks for the quick replies. Let me shortly describe what I want:
> >
> > First, I sorted all stocks according to their annually market cap in 5
> size portfolios = sizep5. Now, I need to create subportfolios; for each
> sizep5 (1 to 5) I want to create 5 subportfolios according to the annually
> BM-ratios of each company in each year. The breakpoints are considered with the
> condition bm<bm4 (which will change to bm<bm8 & bm>bm4 etc.).
> >
> > So, I have 3 conditions to consider: company Y in which sizeportfolio,
> reveals what BM-ratio in which year. Three conditions are too much for me,
> so I decided to the breakpoint condition manually, but I would like to
> automate the others.
> >
> > So, I need a variable which indicates first, what sizep5 portfolio the
> company belongs to, and then what bmp5 portfolio it belongs to. I cannot
> combine these two variables (would be the simplest way!) because I have to
> consider these breakpoints. Is the approach clearer?
> >
> > How does Stata know that forval i = 1/5 corresponds to the sizep5
> variable?
> >
> > I apply 1975/2012 in the looping formula because with
> >
> > generate sizebmp5=11 if bm<bm4 & sizep5==1 & timeyear==1975
> >
> > I only build subportfolios for 1975, but if sizep5 is other than 1,
> Stata should also consider 1975.
> >
> > Many thanks for your support!
> >
> >
> >
> >
> > -------- Original-Nachricht --------
> >> Datum: Wed, 4 Jul 2012 15:19:37 +0100
> >> Von: Nick Cox <njcoxstata@gmail.com>
> >> An: statalist@hsphsun2.harvard.edu
> >> Betreff: Re: st: foreach and forvalues combined
> >
> >> Regardless of whether you are right, the first problem Stata will see
> is
> >> that
> >>
> >> foreach i of sizep5 1/5
> >>
> >> is not a legal syntax for -foreach-. Look again at the help. It seems
> >> that what you want is just
> >>
> >> foreach i of num 1/5
> >>
> >> or (even simpler)
> >>
> >> forval i = 1/5
> >>
> >> Your inner loop seems unnecessary, as you would do the same thing
> >> regardless of which year it was. Also is 1975/2012 what you intend
> >> rather than 1976/2012?
> >>
> >> More fundamentally, I doubt that even when you fix the Stata syntax
> >> you will get what you want. My hunch is that you want to classify
> >> firms according to what they were in 1975, and then spread the
> >> classification to other years, but if so you won't do it like this.
> >> Also, it seems odd that you keep no (apparent) track here of company
> >> identifiers.
> >>
> >> However, I won't try to guess what you want, not least because I could
> >> be quite wrong. Nevertheless our problem is disentangling what you
> >> want from your mistakes in Stata syntax. So my advice is to set your
> >> syntax aside and show us a simple realistic example of what the data
> >> look like now and what you want to get.
> >>
> >> On Wed, Jul 4, 2012 at 3:02 PM, "Fabian Schönenberger" <sch.f@gmx.ch>
> >> wrote:
> >>
> >> > I am facing the following challenge with my panel data while trying
> the
> >> form different portfolios (double ranking with size and BM-ratio).
> After
> >> generating the variable sizebm5 with:
> >> >
> >> > generate sizebmp5=11 if bm<bm4 & sizep5==1 & timeyear==1975
> >> >
> >> > I want to loop the conditions sizep5== and timeyear==. Sizep5 is a
> >> variable consisting of numbers from 1 to 5 (1 small companies, 5 large
> >> companies). Timeyear is the time variable, 1975 to 2012. I am trying
> the following:
> >> >
> >> >
> >> > . foreach i of sizep5 1/5 {
> >> >   2. forvalues t = 1975/2012 {
> >> >   3. replace sizebmp5=1`i' if bm<bm4 & sizep5==`i' & timeyear==`t'
> >> >   4. }
> >> >   5. }
> >> >
> >> > However Stata says invalid syntax. I guess the problem is with
> >> sizebmp5=1`i' -> 1 stands for the first condition bm<bm4 (I will change
> this
> >> condition manually and then switch to 2 and so on). With `i' I want the
> variable
> >> signalling the corresponding sizep5 portfolio. Because this variable
> changes
> >> from 1 to 5, I need the term `i'; I also tried 1_`i' but it did not
> work.
> 
> *
> *   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