Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: How to use functions within variable names

 From Jakob Fischer <[email protected]> To "[email protected]" <[email protected]> Subject Re: st: How to use functions within variable names Date Fri, 19 Aug 2011 02:28:01 +0100 (BST)

```c is already the largest value for every group. It was created in a similar way to your suggestion.

-rowsort- would have saved my time, if I'd knew about it before

I solved the process by doing it for the first observation, moving it to the last position and repeating it until the original first observation returns

*----------- begin example -------------
clear
generate obs= _n
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
generate obs= _n
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

//c being the largest amount of non-empty a-variables within a group
//Group 1: c=3 because of observation 1
//Group 2: c=2 because of observation 6

gen done=.
gsort -c
while done==. {

set obs `=_N+1'
forvalue i=0/2 {
replace b`i'=a`=c-`i'' in 1
}
foreach var in a0 a1 a2 a3 c group result0 result1 result2 b0 b1 b2 obs {
replace `var'= `var'[1] in l
}
replace done=1 in l
drop in 1
}

drop done
sort obs
*------------ end example --------------

I'm sure there is a simplier way to achieve this, but I'm satisfied with the result. I hope that it will work with my real data too.