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]

RE: st: Need to read a value in a stata table into a variable

From   "Subramanian, Hemang" <>
To   "" <>
Subject   RE: st: Need to read a value in a stata table into a variable
Date   Mon, 28 Jan 2013 21:57:43 +0000

To restate the question in one line - 
   "read each variable value in an observation into a separate local variable". This is necessary for looping across two conditions. 

 Here's exactly what I need to accomplish in stata:
    I have two datasets - (1) called the auction_date and (2) called the allauctions datasets. 

 The auction_date dataset has the following format: (This has about 40000 rows)
 ID auction_code auction_date
 ID is the unique identifier and is a number. 

 The second dataset is the allauctions datasets which has the following format . This has about 42 million rows.
 auction_code vehicle_price auction_date vehicle_make_id

The problem statement is as follows:
  For each auction_date in the auction_date dataset, I need to find the mean standard deviation of all vehicle_prices during the month of auctions across all auctions (from the allauctions dataset), matched by vehicle_make_id

The code which failed was as follows:

   foreach var in auction_code sale_date_orig id {
                local var`i' = `var'
        	display var`i'
		`i' = `i' + 1 

 I also tried various combinations of foreach &  forvalues, attempting to read the values into the same. 

My algorithm(psuedo-code) to solve this problem is as follows: - This still needs work though...
file open resultfile using "resultPriceVar.txt", write append
file write resultfile "auction_code" _tab  "sale_date_orig" _tab "mean standarddev" 
For each id in auc_sale {
local sdo = sale_date_orig 
local ac = auction_code
load  allauctions
keep  if (sale_date_orig >= sdo & sale_date_orig <= sdo + 30) [ This is why  I need the exact value of the variable stored in another local variable  ] 

by vehicle_make_id auction_center: gen msppac = mean(vehicle_sales_price)
by auction_center vehicle_make_id: gen dup = _n
drop if dup > 1
by vehicle_make_id: egen std_dev = std(msppac)
by auction_center: egen Price_variance = mean(std_dev)
file write resultfile auction_code _tab sale_date_orig _tab Price_variance


From: [] on behalf of Nick Cox []
Sent: Monday, January 28, 2013 4:32 PM
Subject: Re: st: Need to read a value in a stata table into a variable

This kind of posting will not evoke precise, useful replies.

1. When you say that you have a table, it appears that you are
referring to a dataset. When you talk about rows, you mean
observations. When you talk about local variables, you mean local
macros. I encourage you to use Stata terminology on Statalist.

2. You say that code you represent by multiple dots does not work and
elicits only the first observation. I can make a few wild guesses at
what you are doing, but this is not a puzzle list. Please show us the
exact code that is failing in your eyes.

3. You assert that you need to loop over observations "to perform
further analytics". We can have no idea what you mean precisely, but
that's likely to be quite wrong. The vast majority of Stata problems
do not involve explicit loops over observations.

My best advice is that you describe the problem you want to solve.


On Mon, Jan 28, 2013 at 9:02 PM, Subramanian, Hemang
<> wrote:

>   I have a stata table in the following format:
>  <ac_code> <sale_date> id
>   1234    1Jan2003    1
>   1235    2Jan2005     2
>  Id is the unique identifier for each row. Is it possible to read the  values of ac_code and sale_date into a local variable say "code" and "sd".
>  I tried using foreach var in ac_code sale_date{...
> ....
> ...
> }
>  each time  this logic returns only the first row.. in the table.
>      I need to loop through the entire stata table of approximately 40000 rows, then  use the values stored in local variables "code" & "sd"  to perform further analytics on another larger .dta file (stata file).
*   For searches and help try:

*   For searches and help try:

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