Announcement

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

  • Referring to the last Variable in A Dataset

    I'm working with Johns' Hopkins COVID-19 data. It comes in wide format and I must reshape it. The issue however is that the dataset is updated (daily?) and the name of the last variable changes every day. So, for the outcome data of interest, the data are v12, v13, v14... until today which is v645.

    The approach I've tried so far is simply [Note, I use the user contributed greshape by Mauricio Caceres Bravo]

    Code:
    import delim "https://tinyurl.com/uynhaxd", clear // Imports cases data
    
    cap which greshape
    
    if _rc {
        
        ssc install gtools
        gtools, upgrade
    }
    
    qui: ds v12-v645
    
    loc vars `r(varlist)'
    
    local nwords :  word count `r(varlist)'
    
    disp `nwords'
    
    cls
    
    forv i = 1/`nwords' {
           
        loc a: word `i' of `nwords'
        loc b: word `i' of `vars'
        
        qui: rename `b' day_`i'
    }
    
    drop if fips ==.
    
    qui: greshape long day_, i(fips) j(num) string
    
    rename (num day_) (date cases)
    
    destring date, replace
    
    sort fips date
    
    replace date = date+21935
    format date %td
    Well...... what if I don't want to have to continuously check whatever the name of the last variable is? What if I want Stata to check? So I looked it up.


    And as a result, I tried
    Code:
    import delim "https://tinyurl.com/uynhaxd", clear
    
    cap which greshape
    
    if _rc {
        
        ssc install gtools
        gtools, upgrade
    }
    
    ** The code recommended in the link
    
        des
    
        loc lastvar: word `c(k)' of `r(varlist)'
    
    **
    qui: ds v12-`lastvar'
    
    loc vars `r(varlist)'
    
    local nwords :  word count `r(varlist)'
    
    disp `nwords'
    
    cls
    
    forv i = 1/`nwords' {
           
        loc a: word `i' of `nwords'
        loc b: word `i' of `vars'
        
        qui: rename `b' day_`i'
    }
    
    drop if fips ==.
    
    qui: greshape long day_, i(fips) j(num) string
    
    rename (num day_) (date cases)
    
    destring date, replace
    
    sort fips date
    
    replace date = date+21935
    format date %td
    This returns an error of
    Code:
    nothing found where name expected
    r(198);
    Well, I traced it, and essentially the issue is that
    Code:
        des
        loc lastvar: word `c(k)' of `r(varlist)'
    Isn't capturing the last variable, v645, as it's supposed to. Is there a different or better way to do this? A bug in my code I'm not seeing? I guess I could keep looking at the last variable, but I'd really appreciate knowing how to have Stata do this in context.

  • #2
    Code:
    des

    is describe. You want


    Code:
    ds

    Comment


    • #3
      Alternatively,

      Code:
      describe , varlist

      Comment


      • #4
        findname from the SJ has a handle for this

        Comment


        • #5
          Code:
          sysuse auto, clear
          ds _all
          loc vars `r(varlist)'
          local nwords :  word count `r(varlist)'
          local lastvar : word `nwords' of `vars'
          di "The last variable is: `lastvar'"

          Comment


          • #6
            No additional locals are needed as the scalar c(k) is already stored in creturn.

            Code:
            sysuse auto, clear
            qui ds
            di word("`r(varlist)'", `c(k)')
            Res.:

            Code:
            . di word("`r(varlist)'", `c(k)')
            foreign

            Comment


            • #7
              Okay thank you all so much, these work well!

              Comment


              • #8
                Hi! I hope my posts fits in here if not apologizes [please indicate where to post]. I am not a pro in Stata but here I try to depict my issue:

                I have variables indicating if subject is alive (0) or dead (1) in variable names such as Month1, Month2, Month3, etc.
                I would like to replace the value of 1 with missing (or any other value) for all variables in this varlist that repeat the code 1 but not for the one when the event was observed the first time (code 1 was recorded 1st). Say I have Month1 =0, Month3 = 0, Month6 = 1, Month12 =1, Month24 =1. Please note I also have situation such as:
                Month1 = 0, Month 6 =0, Month12 =0, Month24 =1.
                How can I create a code that changes the value of 1 with any other value (.) if the previous variable has already code 1?

                Many thanks!

                Comment


                • #9
                  Here is #4 exemplified.


                  Code:
                  . sysuse auto, clear
                  (1978 automobile data)
                  
                  . findname, col(-1)
                  foreign
                  .

                  Comment


                  • #10
                    Andrew, Daniel, Jared and, of course, Nick: you guys are Stata gods! thank you so much!

                    Comment


                    • #11
                      Lol I don't know if I'd say THAT exactly about me. I mean I write ado code, so I guess there's that, but some folks on here do works of wizardry in Stata

                      Comment

                      Working...
                      X