Hello

I'm new to programming and I'm stuck trying to do a simple task within a program. How do you perform functions of combinations of individual variables from a varlist in a program? I know this is simple to do using "generate "but I'm trying to write a generic program for displaying the ttest results for a crossover trial.

This is a common set up with y1 being the results from the first period and y2 the results in the second period after the patients have "crossed-over" .In period 1 group1 patients take drug A and drug B in period 2. In period 1 group 2 patients take drug B and drug A in period 2. An example dataset is attached, with the y results being Forced Expiratory Volume, a simple test of respiratory function.

The aim of the study is to look at period, group and treatment (drug) effects. There is simple code to do this:

Code:
 ************************** 
* 3. The t-test analysis *
**************************

* Create the three new variables needed for the t-test analysis
use FEV_data_wide, clear
gen total = y1 + y2
gen diff  = y1 - y2
gen     cross_diff = y1 - y2 if group == 1
replace cross_diff = y2 - y1 if group == 2
save FEV_data_ttest, replace

/* 3.1  T-test for a difference in carry-over effects */ 
use FEV_data_ttest, clear 
ttest total, by(group) 

/* 3.2  T-test for a difference in treatment effects */ 
use FEV_data_ttest, clear 
ttest diff, by(group) 
* NOTE: the point estimate and CI produced from the code
* above need to be divided by two (alternatively, see the CI section below)

/* 3.3  T-test for a difference in period effects */ 
use FEV_data_ttest, clear 
ttest cross_diff, by(group) 
* NOTE: the point estimate and CI produced from the code
* above need to be divided by two (alternatively, see the CI section below)
More for practice than anything else, I would like to write a simple generic program to do these calculations but I can't see how to combine the separate variables from a varlist in a program file.

Something like this but it doesn't work:

Code:
 *!crosshelp.ado
program define crosshelp,rclass byable(recall)
version 16.1
syntax varlist(min=2 max=2 numeric) [if] [in]
tempvar total diff cross_diff
scalar `total'=`var1' + `var2'
scalar `diff'=`var1'-`var2'
scalar `cross_diff' = `var1' - `var2' if group == 1
replace `cross_diff' = `var2'- `var1' if group == 2
 end
This does give the sum using summarize and a recall but it's not what I need:

Code:
 *!adder.ado
program define adder,rclass
version 16.1
syntax varlist
qui summarize,detail
return scalar n=r(sum)
 display in smcl as text _newline(1) "total = " return(n)
 end
Hints on how to get started with this would be appreciated.

Regards

Chris