Bookmark and Share

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


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

RE: st: inputting data via a series of nested loops


From   "Ben Hoen" <bhoen@lbl.gov>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: inputting data via a series of nested loops
Date   Wed, 24 Jul 2013 12:58:02 -0400

Thank you Steve, Sergiy and Matt.  Your advice was perfect.  It worked
great.

Ben  



Ben Hoen
LBNL
Office: 845-758-1896
Cell: 718-812-7589


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Steve Samuels
Sent: Tuesday, July 23, 2013 10:28 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: inputting data via a series of nested loops

Issues that a close look at the -postfile- and -foreach- Manual entries
would have prevented.
1. Permanent postname (not fatal)
 "We recommend that postname always be obtained from tempname." (not fatal)
2. You failed to identify name as a string variable.
3. Correspondingly, you need quotes to refer to the string elements of macro
a.

Steve

This works.

*=========================================

tempname myfile
postfile `myfile' str10 name time place using myfile, replace
local name "a b c"   //
foreach n of local name  {
       forvalues time = 1/2 {
	       
              forvalues place = 1/2 {
              post `myfile' ("`n'") (`time') (`place')
              }
       }
}

postclose `myfile'
use myfile, clear
list
*===========================================



On Jul 23, 2013, at 8:52 PM, Ben Hoen wrote:

Darn, I was pretty optimistic this would work.  Thanks Matt.  But, alas, I
have a bug I cannot immediately find.  (It might be too late to do so...)

The ado:
*=========================================
postfile myfile name time place using myfile.dta, replace
local name "a b c"
foreach n of local name  {
       forvalues time = 1/2 {
               forvalues place = 1/2 {
               post myfile (`n') (`time') (`place')
               }
       }
}
*
postclose myfile
*===========================================

resulted in:
*=============================


. do "B:\temp\STD07000000.tmp"

. postfile myfile name time place using myfile.dta, replace

. local name "a b c"

. foreach n of local name  {
 2.         forvalues time = 1/2 {
 3.                 forvalues place = 1/2 {
 4.                 post myfile (`n') (`time') (`place')
 5.                 }
 6.         }
 7. }
a not found
post:  above message corresponds to expression 1, variable name
r(111);

end of do-file

r(111);

*====================================

It seems like the problem might be in the foreach, but I will need to get
back to it in the morning.

Thanks again.  

Ben

Ben Hoen
LBNL
Office: 845-758-1896
Cell: 718-812-7589


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Matthew White
Sent: Tuesday, July 23, 2013 7:55 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: inputting data via a series of nested loops

Hi Ben,

How about -post-?

Best,
Matt

On Tue, Jul 23, 2013 at 7:30 PM, Ben Hoen <bhoen@lbl.gov> wrote:
> Thanks Sergiy,
> 
> I actually did mean -input- as in "input x y".  I can see the problem with
> what I wrote given what you mention about the data immediately following
the
> -input- command.  Is there any way to build a dataset as I had hoped using
> loops of numbers/strings?  For example, can I display, and then use the
> contents of the display for the data I wish to enter?
> 
> I am grasping at straws here, but I have a large patterned dataset
(similar
> to the one I mentioned) that I need to build as a template and it would be
> useful to use loops to build it.  Then again, maybe there is a much
> different way than using -input-...
> 
> Thanks, for any advice you or others might offer.  Best,
> 
> Ben
> 
> Ben Hoen
> LBNL
> Office: 845-758-1896
> Cell: 718-812-7589
> 
> 
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Sergiy Radyakin
> Sent: Tuesday, July 23, 2013 4:13 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: inputting data via a series of nested loops
> 
> The command -input- assumes the data follows immediately in the program:
> 
> input x y
> 10 20
> 20 70
> end
> 
> You are probably confusing it with a command -input- in some other
> languages, like BASIC, where it serves the purpose of requesting
> values from the user.
> 
> In Stata you can use the -display- command for that:
> 
> display "Enter value for X:"  _request(x)
> display `"$x"'
> 
> Best, Sergiy
> 
> 
> On Tue, Jul 23, 2013 at 2:46 PM, Ben Hoen <bhoen@lbl.gov> wrote:
>> Hi all,
>> 
>> I am trying to encourage Stata to enter data into a new dataset via a
loop
>> in an ado file.  I was not able to find anything in the archives that fit
> my
>> situation (though I did find this:
>> http://www.stata.com/statalist/archive/2010-07/msg00425.html).
>> 
>> For example, say I wanted to end up with this as my dataset:
>> 
>> type    place   time
>> a       1       1
>> a       1       2
>> a       2       1
>> a       2       2
>> b       1       1
>> b       1       2
>> b       2       1
>> b       2       2
>> c       1       1
>> c       1       2
>> c       2       1
>> c       2       2
>> 
>> I have tried the following which works:
>> 
>> local name "a b c"
>> foreach n of local name  {
>>        forvalues time = 1/2 {
>>                forvalues place = 1/2 {
>>                display "`n' `time' `place'"
>>                }
>>        }
>> }
>> end
>> *
>> 
>> The results:
>> 
>> 
>> . do "B:\temp\STD08000000.tmp"
>> 
>> . local name "a b c"
>> 
>> . foreach n of local name  {
>>  2.         forvalues time = 1/2 {
>>  3.                 forvalues place = 1/2 {
>>  4.                 display "`n' `time' `place'"
>>  5.                 }
>>  6.         }
>>  7. }
>> a 1 1
>> a 1 2
>> a 2 1
>> a 2 2
>> b 1 1
>> b 1 2
>> b 2 1
>> b 2 2
>> c 1 1
>> c 1 2
>> c 2 1
>> c 2 2
>> 
>> And therefore I hoped the following would work:
>> 
>> input name time place
>> local name "a b c"
>> foreach n of local name  {
>>        forvalues time = 1/2 {
>>                forvalues place = 1/2 {
>>                `n' `time' `place'
>>                }
>>        }
>> }
>> end
>> *
>> 
>> But, for which I get:
>> 
>> 
>> . do "B:\temp\STD08000000.tmp"
>> 
>> . input name time place
>> 
>>          name       time      place
>>  1. local name "a b c"
>> 'local' cannot be read as a number
>>  1. foreach n of local name  {
>> 'foreach' cannot be read as a number
>>  1.         forvalues time = 1/2 {
>> 'forvalues' cannot be read as a number
>>  1.                 forvalues place = 1/2 {
>> 'forvalues' cannot be read as a number
>>  1.                 `n' `time' `place'
>> '`' cannot be read as a number
>>  1.                 }
>> '' cannot be read as a number}
>>  1.         }
>> '' cannot be read as a number}
>>  1. }
>> '' cannot be read as a number}
>>  1. end
>> 
>> . *
>> .
>> end of do-file
>> 
>> 
>> Any ideas how to correct?
>> 
>> Thanks, in advance,
>> 
>> Ben
>> 
>> 
>> Ben Hoen
>> Staff Research Associate
>> Lawrence Berkeley National Laboratory
>> Office: 845-758-1896
>> Cell: 718-812-7589
>> bhoen@lbl.gov
>> http://emp.lbl.gov/staff/ben-hoen
>> 
>> Visit our publications at:
>> http://emp.lbl.gov/reports/re
>> 
>> Sign up for our email list to receive publication notifications at:
>> 
> 
https://spreadsheets.google.com/a/lbl.gov/spreadsheet/viewform?formkey=dGlFS
>> 1U1NFlUNzQ1TlBHSzY2VGZuN1E6MQ
>> 
>> 
>> 
>> 
>> 
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
> 
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/



-- 
Matthew White
Senior Project Associate
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
www.poverty-action.org
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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