Announcement

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

  • How to get a list of variables in the varlist along with their index number in the varlist?

    Hello,

    I know that the code below would give the total number of variables contained in my varlist:

    Code:
    count `varlist'
    But is there any way that I can generate a list of variables in the current varlist along with their index number in the varlist (so that I can look up the index number of the variable of interest from the list, based on the variable name)?

    Thank you,

  • #2




    Not so; that really doesn't count the number of varlables in any varlist. If the local macro varlist is populated, it is illegal. If varlist is empty, it counts the number of observations.


    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . unab varlist : *
    
    . h macro
    
    . di "`varlist'
    make price mpg rep78 headroom trunk weight length turn displacement gear_ratio foreign
    
    . di wordcount("`varlist'")
    12
    
    . di `: list posof "mpg" in varlist'
    3
    I can't recollect ever using an index number of a variable in the dataset.

    Comment


    • #3
      Hello,

      Thank you very much for your reply.
      I am new to Stata and there are many things that I am not familiar with.

      How can I save the results of -di wordcount("`varlist'")- and -di `: list posof "mpg" in varlist'- under local macros?

      Thank you,

      Comment


      • #4

        Code:
        local wanted = wordcount("`varlist'")
        and so on.

        Comment


        • #5
          Because Dominique is new to Stata, here is an additional word of caution: the position of a variable in a varlist is not necessarily the position of that variable in the dataset. Dominique might want to keep this in mind.

          Best
          Daniel

          Comment


          • #6
            Thank you both!

            Comment


            • #7
              I have some additional question.
              If I want to store the result of -`: list posof "mpg" in varlist'- in a local macro, am I right in executing the code below?

              Code:
              local exclamMarkIndex = `: list posof "t_EXCLAMATIONMARK" in varlist'
              What I want to do is I want my local macro 'exclamMarkIndex' to store the position of the variable 't_EXCLAMATIONAMRK' in the varlist, but this doesn't seem to do the trick...

              Thank you,

              Comment


              • #8
                You have mentioned earlier that you are new to Stata. What you are trying to do here does not remind me of the typical workflow in Stata. You are free to do whatever you like, but you might benefit from telling us why you want this and what your ultimate goal is. Although I might be wrong, I feel we might be running into the XY Problem here.

                Best
                Daniel

                Comment


                • #9
                  Hello,

                  Thank you for your reply.

                  I have this huge dataset that has over 1000 variables, and I am trying to use the Stata-Python interface to build a Multinomial Naive Bayes model with the dataset.
                  As the Multinomial Naive Bayes is only feasible on Python, I have been trying to transfer bunch of Stata variables into Python. Normally this kind of task is accomplished like the code below:

                  Code:
                  # python code
                  X = pd.DataFrame(Data.get(<names of the variables that I want to import>)))
                  But the problem here is, I cannot list all the names of the X variables that I want to import because there are just too many of them. So I am trying to use the property that, in my Stata dataset, all the X variables that I want to use in analysis starts from the column where the variable -"t_EXCLAMATIONMARK"- is located all the way to the last column.

                  and this is why I want to get the varlist index of "t_EXCLAMATIONMARK", because then I can just do:

                  Code:
                  # python code
                  X = pd.DataFrame(Data.get(var = list(range(exclam_mark_pyIndex, var_count))))
                  :S I don't know what else I can do...

                  Thank you,

                  Comment


                  • #10
                    perhaps I could try to break the original Stata dataset into smaller chunks "on Stata", which I do not know how.

                    Comment


                    • #11
                      I see. I trust you have explored Stata's bayes suit and you are confident that Python is the way to go. I do not have access to Stata 16 but it would I would be surprised if interation of Python into Stata did not provide easy access to Stata datasets, etc.

                      Anyway, I am really just getting started with Python but it appears as if Python is closer to Mata than to Stata. Perhaps you would find switching to Mata simpler? For example

                      Code:
                      . sysuse auto
                      (1978 Automobile Data)
                      
                      . mata : st_varindex("mpg")
                        3
                      is how to get the position of a variable in a Stata dataset from Mata.

                      Hope that helps.

                      Best
                      Daniel

                      Comment

                      Working...
                      X