Bookmark and Share

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

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

st: Question on loop over variable

From   Guo Chen <>
Subject   st: Question on loop over variable
Date   Thu, 28 Jul 2011 20:12:23 -0500

Hi, all:

I wanna calculate monthly average purchase price for all the products
in my data set, so I need loop over all values of variable "itemid".

I checked  and the
following code is the best I can do, but I still got error messege:

file C:\DOCUME~1\MPTMP026\LOCALS~1\Temp\ST_0000000t.tmp not found

Can you please take a look at my code?

==========begin code=========
use data, replace

tempfile data

levelsof itemid, local(levels)

foreach l of local levels{
use "`data'", clear
keep if itemid == "`l'"
bys monthly: replace total_purchase_price = sum(extendedcost)
bys monthly: replace total_quantity = sum(itemqty)
replace average_purchase_price = total_purchase_price / total_quantity

bys monthly: gen i = [_n]== 1
drop if i != 1
outsheet using "C:\Documents and Settings\MPTMP026\Desktop\new\`l'.txt"

Guo Chen
University of Minnesota
Dept. of Applied Economics
125-E Ruttan Hall
1994 Buford Avenue
St. Paul, MN 55108
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index