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, statalist.org is already up and running.


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

st: How to insert scalars (elements from Stata's matricies) as locals' values in the 'forvalues' command?


From   Charalambos Karagiannakis <karagiannakis.charalambos@ucy.ac.cy>
To   statalist@hsphsun2.harvard.edu
Subject   st: How to insert scalars (elements from Stata's matricies) as locals' values in the 'forvalues' command?
Date   Fri, 19 Oct 2012 10:19:21 +0300

 

Hello dear Statalist friends. I am trying to write a loop to create three new variables that will contain the mean of the initial three variables ‘_Icity_3’,  ‘rooms’ and ‘sqrf’ for every selected value (i = 160(1)209) of the ‘time_quarter’ variable. The following loop worked and implemented successfully what I wanted, however I would like to automate the process a bit more and here is where I need your help.

 

local z = 1

foreach X of var  _Icity_3  rooms  sqrf {

generate m_`X' = .

forvalues i = 160(1)209 {

sum `X' if time_quarter == `i'

return list

replace  m_`X' = r(mean) in `z'

local z = `z' + 1

}

local z = 1

}

 

In the fourth line of the above code, the values 160 and 209 correspond to the minimum and maximum value of the ‘time_quarter’ variable respectively. Now, every time I update my sample including new observations these two figure might change. That is why I would like to find a way to insert those two number as parameters let’s say. I tried the following (line 4 of the above code was replaced by lines 4&5 in the code below) but it didn’t work. It returns an ‘invalid syntax’ error.

 

local z = 1

foreach X of var  _Icity_3  rooms  sqrf {

generate m_`X' = .

sum time_quarter

forvalues i = [r(min)](1)[r(max)] {

sum `X' if time_quarter == `i'

return list

replace  m_`X' = r(mean) in `z'

local z = `z' + 1

}

local z = 1

}


Is it feasible to parameterize locals and if yes, how would you do it?

PS. I apologize if the subject wasn’t very clear.

Thank you in advance guys.

 

Harry K..

 



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