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: How to combine variables in network data - can I loop over stubs?


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: How to combine variables in network data - can I loop over stubs?
Date   Thu, 21 Apr 2011 00:45:14 +0100

That may be your ideal syntax but the documentation for -foreach- is
clear. There is no such legal form.

foreach stub in AAA BBB {
...
}

is perfectly legal.

To get a list of all such stubs, you could try:

unab stubs : *_0
local stubs : subinstr local stubs "_0" "", all

See http://www.stata.com/support/faqs/data/reshape3.html for explanation.

You can then go

foreach stub of local stubs {
...
}

In short, good news: you don't need a new syntax. Existing syntaxes
can be adopted.

Your mapping 0, 1, 2 -> 0, 2, 3 seems a bit strange, but I assume
that's a typo or a side-issue.

Nick

On Thu, Apr 21, 2011 at 12:23 AM, Brandon Olszewski
<[email protected]> wrote:

> I have a -wide- social network data set that needs some cleaning. It
> contains about 40 different projects, here named AAA, BBB, CCC, etc.
> Each project is connected to every other project at some level, 0 =
> “no familiarity”, 1 = “familiar”, and 2 = “works with a lot”. Rows
> represent projects, and columns represent the level of familiarity a
> project has with others. Here is what the data look like, then:
>
> project AAA_0   AAA_1   AAA_2   BBB_0   BBB_1   BBB_2
> AAA                                                                                     1
> BBB                     0
> Etc.
>
> In the above example, project AAA knows B at a level 1 (familiar), and
> project BBB reports not knowing project AAA (at a level 0).
>
> I want to create variables (total_AAA, total_BBB, etc.) that combine
> columns with the same stub (“AAA”, “BBB”, etc.) so that one new column
> represents the three old columns. The new column would contain a value
> (0, 1, or 2) that was previously represented in wide format across
> three columns. Searching the statalist, the following post is the
> closest I found:
> http://www.stata.com/statalist/archive/2007-03/msg00234.html.
>
> I’m thinking of using a -foreach- command to do it, but I think I
> would need to loop the command over a stub, rather than a varname. I
> also know how to use -renpfix- to change stub names. In my ideal
> world, a solution would look like:
>>>>
> foreach stub of stubname AAA BBB{
>        gen total`stub’=0
>        replace total`stub’=2 if `stub’_1==1
>        replace total`stub’=3 if `stub’_2==2
>        }
> keep project total*
>>>>
>
> I've racked my brain with -reshape- and some other commands I know,
> but the logic eludes me and I am still unsuccessful. Can anyone help
> me?
>

*
*   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