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: How to use functions within variable names


From   Jakob Fischer <jagfish101@yahoo.de>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: How to use functions within variable names
Date   Thu, 18 Aug 2011 07:08:35 +0100 (BST)

Hello, first of all thank you Maarten and Daniel for your input and time.
How rude of me to use that nonsense name, it was solely intended for the email provider. Please excuse this discourtesy.

I tried to keep the example small and simple and focus only on my issue. But here is what I want to do:
I want to reverse sets of variables for groups with different lengths. The example should illustrate it.


*----------- begin example -------------
clear
input a0 a1 a2 a3 c group result0 result1 result2 b0 b1 b2
      .    1    2    3    3    1    3    2    1    .    .    .
      .    4    5    .    3    1    .    5    4    .    .    .
      .    6    .    .    3    1    .    .    6    .    .    .
      .    7    .    .    2    2    .    7    .    .    .    .
      .    8    .    .    2    2    .    8    .    .    .    .
      .    9    10    .    2    2    10    9    .    .    .    .
end
compress

list a1 a2 a3 group c
//The result should be in reversed order, always starting with variable result0. Where c is the biggest "length" of a-variables within a group.

list result0 result1 result2 group c
//This is what the final result should look like
//Group 1: c=3 because of observation 1 
//Group 2: c=2 because of observation 6

forvalue i=0/2 {
replace b`i'=a`=c-`i''
}

list b0 b1 b2 group c
// Observations 4-6 are not correct
*------------ end example --------------

Unfortunately, as stated by Daniel, Stata takes the c value from the first observation for all observations. Using c[_n] is not working either, but I hope to find a solution to that problem.


-Jakob

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