Announcement

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

  • Interactive do File à la C++/Python

    I have 2 datasets: A.dta, and B.dta. I want the same set of codes to run on A & B depending on the dataset that I choose.

    In C++, for example, I could write:

    Code:
    cout<<"Which dataset do you want to use?"
    cin>>dataset
    if (dataset == A)
    {
               //code
    }
    if (dataset == B)
    {
              //code
    }
    Is there a similar thing that I can do with Stata?

    P.S. Asking me to write down 'what I wrote in Stata and what is not working for clarification' would be a non-sequitur because I couldn't find anything which works in such an interactive way in Stata. All I could do is write an analogous C++ code.

  • #2
    Code:
    display "Which data set do you want to use?" _request(dataset)
    use `"$dataset"', clear
    if `"$dataset"' == "A" {
        // CODE TO PROCESS DATA SET A
    }
    
    if `"$dataset"' == "B" {
        // CODE TO PROCESS DATA SET B
    }
    Added: Global macros are hazardous, but that is what -display _request()- creates. So be cautious that global macro dataset does not already exist, nor is any global macro of that name likely to be used by any program you call from within this code, nor any program that might call this one. To be on the safe side it is probably better not to use a mnemonoic name like dataset but to use some gobbledygook that looks like a strong password.
    Last edited by Clyde Schechter; 14 Sep 2019, 11:01.

    Comment


    • #3
      The risk of globals noted in #2 may in this example be adressed by
      Code:
      tempname dataset
      assert `"${`dataset'}"' == "" 
      
      display "Which data set do you want to use?" _request(`dataset')
      
      local dataset ${`dataset'} 
      macro drop `dataset'      
      
      use `"`dataset'"', clear
      ...

      Comment


      • #4
        Thank you so much, Clyde Schechter and Bjarte Aagnes . I've taken elements from both the suggestions. I think this works perfectly.

        (As an addition for anyone who stumbles on this question, for me, writing it like this (as suggested by Mr. Aagnes)
        Code:
         use `"`dataset'"', clear
        or this (as suggested by Mr. Schechter)
        Code:
         use `"$dataset"', clear
        doesn't work. I don't know if it is because of the particular Stata version (I am on Stata IC 15) I am using. What works for me is this

        Code:
         use "$datasetinput", clear
        )
        Last edited by Nilima Pisharody; 14 Sep 2019, 16:20.

        Comment


        • #5
          To avoid defining a global; prefix the (macro) name with an underscore:
          Code:
          display "Which data set do you want to use?" _request(_filename)
          
          use "`filename'"
          
          ...

          Comment


          • #6
            Bjarte Aagnes That is super-cool! I didn't know you could do that.

            Comment


            • #7
              Ditto. Thanks for that.

              Comment

              Working...
              X