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 on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Using a loop to calculate vector differences

From   "Heidi M Pitts" <>
Subject   st: Using a loop to calculate vector differences
Date   Thu, 20 Sep 2012 15:45:36 -0600

Hi listers,
Can anyone help fix my code for the following issue:

I have two vectors:
M=[0, 0, 0, 0, 0, 0, 0, .1, .4, .4, .1]
F=[0, 0, 0, 0, 0, .05, .3, .6, .05, 0, 0]
so n=11 obs for each vector.

I?m trying to calculate the vector difference between each element `i? in M and the whole vector F. So the first vector of differences would be [M1=0 minus 0,0,0,0,0,.05,.3,.6,.05,0,0]. Then I would calculate a second set of differences, M2=0 - same F vector, and stack it under the diff vector.

I have written a simple loop to calculate each difference. But the error message I get is: ?x_male1 not found?. Seems that my code is not recognizing that I want the first element of M to subtract each element of F and populate the vector ?diff?. Can anyone help? Seems that the first issue is that ?x_male1? is not equal to 0, the first element in the vector.

/************ program to calculate difference of distributions***************/
set matsize 100

mkmat x_male
mkmat y_female
mkmat y_fem_inv

set obs 11
gen n=_n
gen diff=.
mkmat diff
forvalues i=1(1)11 {
	gen y=x_male`i'-y_female
	replace diff=y if `i'< n



Thanks for any help!

Heidi M. Pitts
Graduate Student
Department of Economics
University of New Mexico

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index