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: Reshape with prefix using a varlist


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Reshape with prefix using a varlist
Date   Tue, 15 Mar 2011 08:58:47 +0000

Rebecca is correct.

Another way to do it, without a loop:

unab myvars : country1-country244
local myvars : subinstr local myvars " " " @", all

That fixes every variable name except the first, which lacks a
preceding "@", but you can just supply that directly to -reshape-:

reshape ... @`vars', ...

Nick

On Tue, Mar 15, 2011 at 2:29 AM, Rebecca Pope <[email protected]> wrote:
> Richard,
> I know you've gotten what you need to accomplish the task at hand, but
> I'd like to address your original question, in case you may find this
> trick useful at a later date...
>
> If you want a series of the form @var1 @var2 etc, you can always make
> Stata write the bulk of the command for you. I've used variants of
> this for all sorts of purposes (e.g. sticking "i." or "c." in front of
> a list of variables).
>
> /*use this if all countries are countryi*/
> local vars ""
> forvalues i=1/244 {
> local vars "`vars' @country`i'"
> }
>
> /*use this if you have names that vary from the countryi naming convention*/
> unab varlist: country1 - country244
> local vars ""
> foreach var of local varlist {
> local vars "`vars' @`var' "
> }
>
> Then you could submit, using your example, -reshape wide `vars',
> i(inst year) j(LEVEL) string-.
> Hope this helps,
> Rebecca
>
> On Mon, Mar 14, 2011 at 8:51 PM, Richard Murphy
> <[email protected]> wrote:
>>
>> That's a very good point, I hadn't considered going in the other
>> direction (even longer) and then collapsing when need be.
>> Thanks for this, I think i will give this a try.
>>
>> Best
>> Richard
>>
>>
>> On 14 March 2011 18:35, Nick Cox <[email protected]> wrote:
>> > I think I would make this even -long-er if it were mine.
>> >
>> > reshape long country , i(year inst LEVEL)
>> >
>> > Then whatever reductions you want would be -egen- or -collapse- or -contract-.
>> >
>> > Why do I suggest this? You are already imagining that you need to
>> > combine a loop over variables with totals over observations. Think how
>> > much more of that you would need with yet more variables.
>> >
>> > Nick
>> >
>> > On Tue, Mar 15, 2011 at 1:21 AM, Richard Murphy
>> > <[email protected]> wrote:
>> >> The label "country1-country244" represents the remaining 244
>> >> variables, containing the total amount of students from that country
>> >> at that level in a uni-year.
>> >> Are you suggesting something along the lines of
>> >> foreach x in varlist eu uk {
>> >>
>> >>
>> >> On 14 March 2011 18:02, Nick Cox <[email protected]> wrote:
>> >>> Thanks. That clarifies some things but not others. In your example,
>> >>> you have 7 headings but 6 columns of data. What is
>> >>> "country1-country244"? A variable label?
>> >>>
>> >>> The totals for groups are easily computable using -egen, total()- with
>> >>> your present structure.
>> >>>
>> >>> On Tue, Mar 15, 2011 at 12:45 AM, Richard Murphy
>> >>> <[email protected]> wrote:
>> >>>> Hi Nick ,
>> >>>> I appreciate that it looks foolish making a wide dataset even wider,
>> >>>> but I don't think its that bad in this case.
>> >>>> I have university by degree level by year data. For each of these i
>> >>>> have the number of students coming from 244 different countries, along
>> >>>> with total EU and total OS.
>> >>>>
>> >>>> year    instit  LEVEL   os      uk      eu country1-country244
>> >>>> 1994    1       ug              12      146     0
>> >>>> 1994    1       pgr             3       335     3
>> >>>> 1994    1       pgt     1       101     0
>> >>>> 1995    1       ug              7       119     0
>> >>>> 1995    1       pgr             4       300     9
>> >>>> 1995    1       pgt             6       59      17
>> >>>>
>> >>>> There are 3 levels of degree, which i would like to make wide. So that
>> >>>> I would have a panel dataset for universities over time. The reason
>> >>>> why i want to do this is that I want to calculate the cross
>> >>>> subsidisation that occurs between the degree levels, and for this I
>> >>>> need the totals for each within an observation.
>> >>>>
>> >>>> I need to do this for all the countries as i'm using a Card
>> >>>> Shift-Share approach as an instrument for changes in overseas numbers,
>> >>>> which i would like to define in various different ways throughout the
>> >>>> analysis (EU, NonEU, Asia, ect).
>> >>>>
>> >>>> And i would like the prefix, so that the variable names fit into my
>> >>>> pre existing do files for the analysis.
>> >>>>
>> >>>> Best regards
>> >>>> Richard
>> >>>>
>> >>>>
>> >>>> On 14 March 2011 17:24, Nick Cox <[email protected]> wrote:
>> >>>>> Please tell us directly more about your dataset and why you think
>> >>>>> -reshape wide- is a good idea. From what you say it just make most
>> >>>>> analyses more difficult. Also, how many variables will you end up
>> >>>>> with?
>> >>>>>
>> >>>>> Nick
>> >>>>>
>> >>>>> On Tue, Mar 15, 2011 at 12:13 AM, Richard Murphy
>> >>>>> <[email protected]> wrote:
>> >>>>>> Hi all,
>> >>>>>> I want to reshape long data to wide, putting the 'j' string variable
>> >>>>>> at the begging of the stub.
>> >>>>>> This would be fine if I have a limited number of variables as i can
>> >>>>>> just use the @ function.
>> >>>>>> reshape wide @var1 @var2 @var3 @var4, i(instit year) j(LEVEL) string
>> >>>>>>
>> >>>>>> But this does not work if i use the variable list functionality of
>> >>>>>> reshape. I would like to know if their is an easier way of doing this,
>> >>>>>> rather than typing in all 244 variables preceded by @.
>> >>>>>> reshape wide @var1-@var244, i(instit year) j(LEVEL) string

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index