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]
SV: st: Using values in an variable to save parts of an dataset
From
Andreas Dall Frøseth <[email protected]>
To
"[email protected]" <[email protected]>
Subject
SV: st: Using values in an variable to save parts of an dataset
Date
Tue, 30 Apr 2013 14:57:07 +0000
I tried the approach where you exploited a feature of -use-, and it seems to work just fine.
Thank you.
-Andreas
________________________________________
Fra: [email protected] [[email protected]] på vegne av Nick Cox [[email protected]]
Sendt: 30. april 2013 16:44
Til: [email protected]
Emne: Re: st: Using values in an variable to save parts of an dataset
Your local references are the wrong way round.
foreach industryid in `industryids' {
keep if industryid==`industryid'
save industry_`industryid'
}
You want each statement inside the loop to take on each individual value.
This is all assuming that -industryid- is numeric.
However, even with this fixed your loop won't work. Second time round,
all you have in memory is the first subset resulting from -keep-.
But then you can exploit a feature of -use-:
levelsof industryid, local(industryids)
foreach industryid in `industryids' {
use mydata if industryid==`industryid', clear
save industry_`industryid'
}
Nick
[email protected]
On 30 April 2013 15:33, Andreas Dall Frøseth
<[email protected]> wrote:
> Dear all.
>
> I'm trying to divide my dataset into pieces based on the values in an variable. My set is an large panel data, containing a number of companies. Each company has a value in the variable "industryid", which allows me to identify what industry it operates in.
> I am now trying to divide this large dataset into smaller sets for each single industry.
>
> The reason why I'm struggling is that I wish to apply this separation for a number of different sets, which might contain a different amount of industries, without having to identify the values in the industry-variable myself.
> I have tried to make a macro with the values using the "levelsof" command, and then apply it with:
>
> foreach industryid in `industryids' {
> keep if industryid==`industryids'
> save industry_`industryids'
> }
>
>
> But this runs back as "invalid '10'".
>
> Any suggestions on how to do this?
> Forgive me if this is way to basic...
>
> Your feedback will be appreciated.
>
> -Andreas
> *
> * 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/