Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Input+end inside a Program

    Hi,

    I like using input/end, in order to easily type data,
    Code:
    clear
    input float var1
    1
    2
    3
    4
    5
    end
    instead of set obs/replace (too verbose!)
    Code:
    set obs 6
    replace var1 = 6 in 6
    however, when inside a Program , the "end' clause , ends , also, the Program, such as in:

    Code:
    cap program drop toy
    program define toy, nclass
    clear
        input float var2
        1
        2
        3
        4
        5
        end
    list
    end
    
    . end
    command end is unrecognized
    r(199);
    
    end of do-file
    
    r(199);
    Is there any way using "input" in that circumstance ?

  • #2
    Sorry to say, no.

    Even though your "end" is meant to terminate the input command, at the time Stata is parsing the program definition, the fact that the "end" belongs to the input command and not to the loop is not recognized, and causes Stata to react incorrectly.

    For slightly clearer presentation of the problem, here is the full output from your example showing the subsequent list command outside the intended definition of the program: that is the indication that the first end command has terminated the program definition.
    Code:
    . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD10505.000000"
    
    . cap program drop toy
    
    . program define toy, nclass
      1. clear
      2.     input float var2
      3.     1
      4.     2
      5.     3
      6.     4
      7.     5
      8.     end
    
    . list
    
    . end
    command end is unrecognized
    r(199);
    
    end of do-file
    
    r(199);
    
    .

    Comment


    • #3
      William Lisowski has given excellent advice about the problem. Here I will add some possible workarounds.

      Originally posted by Luis Pecht View Post
      instead of set obs/replace (too verbose!)
      If verbosity is the only issue, then simply prefix each -replace- statement with -quiet:- and it will suppress the printing to the Results window.

      However, I don't think this is generally the problem, but rather having any actual data wot work with when you are testing a program. If you are lucky and there is some structure to the data that reduces to a simple formula, you can program the dataset to be created. For example, to replicate a set of observations taking values from 1 to N, you can do this.

      Code:
      cap program drop dostuff
      program dostuff
        version 17
        syntax , [n(integer 6)]
        drop _all
        set obs `n'
        qui gen `c(obs_t)' num = _n
        list
      end
      
      dostuff, n(5)
      Results

      Code:
           +-----+
           | num |
           |-----|
        1. |   1 |
        2. |   2 |
        3. |   3 |
        4. |   4 |
        5. |   5 |
           +-----+
      ( An aside: In the example above, I used the system macro -c(obs_t)- which selects the best integer datatype to count observations from 1 to N, where N is the number of observations in the dataset. The possible datatype could be byte, int or long. You don't need to use that, but be aware that it exists. If you had a formula that didn't simply count, but return decimal values, that -float- or -double- types would be better suited. )

      The code above solves a pretty specific situation and it isn't always possible to define a dataset with a set of equations. In that case it's best to simply define the test data outside of, and prior to, the program call. Here you can use -input- or whatever method brings data into to Stata. In your program, you would then assume that you already have data and can work with it accordingly.

      Comment

      Working...
      X