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   Sergiy Radyakin <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: inputting data via a series of nested loops
Date   Tue, 23 Jul 2013 20:49:34 -0400

If Stata's -edit- command had retained it's original modal mode, you
could do something like:
  clear
  set obs 10
  gen x=.
  gen y=.
  edit
  list

But even under version control you can't reproduce the same behavior
as the above code was running in e.g Stata 9.2. With that you could
create a grid for your dataset, then display it to the user to
populate (or prepopulate it and display for the user to edit).

Typing a "large patterned dataset" by hand is generally a bad idea. To
reproduce it somebody would need to type exactly the same, and that's
a problem.

In general you can assign a numeric value anywhere in the dataset with:
mata st_store(obsindex,varindex,newvalue)

Note that the -input- statement wants all the variables within one
line (the whole observation is to strict, since it can merge data to
existing variables). What you specify is a random-access pattern.

Here is a random-access coordinate-based editor (enter 0 for obs
number to exit):
***********************************************************************
clear
set obs 10
forval i=1/10 {
  generate x`i'=.
}
while 1 {
  list
  display "Input observation index: " _request(oindx)
  if ($oindx<1) continue,break
  display "Input variable number: " _request(vindx)
  display "Input new value: " _request(val)
  mata st_store($oindx,$vindx,$val)
}
list
***********************************************************************

Only writing all of the above I can see the pattern you want:
for all values of the first (string) var produce all possible
combinations of the other two (numeric) vars taking values 0/1

So here is a combinator:

clear

local types "a b c d"
local places "0 1"
local times "0 1"

set obs `=`:word count `types''*`:word count `places''*`:word count `times'''

generate type=""
generate place=.
generate time=.

list

local i=1
foreach onetype in `types' {
  foreach oneplace in `places' {
    foreach onetime in `times' {
      quietly {
          replace type="`onetype'" in `i'
          replace place=`oneplace' in `i'
          replace time=`onetime' in `i'
      }
      local i=`i'+1
    }
  }
}

list, clean noobs

Produces:

   type   place   time
       a       0      0
       a       0      1
       a       1      0
       a       1      1
       b       0      0
       b       0      1
       b       1      0
       b       1      1
       c       0      0
       c       0      1
       c       1      0
       c       1      1
       d       0      0
       d       0      1
       d       1      0
       d       1      1


The task above now seems pretty standard, and there might be a
standard command available for it, but it was faster to write from
scratch then trying to find it.

Best, Sergiy Radyakin












On Tue, Jul 23, 2013 at 7:30 PM, Ben Hoen <[email protected]> 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: [email protected]
> [mailto:[email protected]] On Behalf Of Sergiy Radyakin
> Sent: Tuesday, July 23, 2013 4:13 PM
> To: [email protected]
> 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 <[email protected]> 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
>> [email protected]
>> 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/
*
*   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