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: RE: Saving loop index value

From   "Martin Weiss" <>
To   <>
Subject   st: RE: Saving loop index value
Date   Thu, 8 Apr 2010 21:21:38 +0200


You can -save- it with the dataset. -postfile-, btw, might be a smarter
choice for your problem.

forv i=1/10{
set obs 1
gen byte myvar=`i'
save myfile`i', replace
drop _all

//example file # 7
u myfile7


-----Original Message-----
[] On Behalf Of Hollis,Michael E
Sent: Donnerstag, 8. April 2010 21:09
Subject: st: Saving loop index value


I want to "keep" the value of i which indexes the "forvalues" loop in
the loop below.  I've tried including it as a macro variable (i.e., `I')
on my keep statement.  I've also tried assigning it to a new variable
and then keeping the new variable.  Neither has worked.  Can someone
tell me how to keep i in my output dataset?

Thanks in advance.

P.S. I'm new to using macros and programming in Stata.

#delimit ;
   clear all;
   log using test.log, replace;
   set more 1;
   insheet using CAT_scenario_1.csv;          
   drop cat_scenario county;
   rename max_temp_degf maxt;
   rename precip_in prcp;
   forvalues i = 0/45 {;                       
	 keep if offset==`i';
	 collapse (mean) prcp maxt, by(period);
	 do bm9304_6;
	 drop sdci_bp grup_bp;
	 keep year *_bp;
	 save scenario`i', replace;

Michael Hollis, Ph.D
Resource and Analysis Group
Water Resource Management
Metropolitan Water District of Southern California
voice: 213-217-7228  fax: 213-2176119

*   For searches and help try:

*   For searches and help try:

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