Announcement

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

  • Using a local macro that contains a list of random numbers to avoid repetitive typing

    Hi Stata community.

    I am relatively new to Stata. I am doing a project and looking at individuals with random ages. I have to constantly list these numbers whenever I run a new command. I am trying to use a local macro but I have never used it before. If someone could help, I would appreciate it. Below is an example with comments.


    Code:
    clear all
    *Call in sample data
    sysuse nlsw88.dta
    
    *Count if someone is either 34, 37, 41, 45, or 46
    count if inlist(age, 34, 37, 41, 45, 46)
    
    *Count if someone is either 34, 37, 41, 45, or 46 and race is 2
    count if inlist(age, 34, 37, 41, 45, 46) & race==2
    
    *Count if someone is either 34, 37, 41, 45, or 46 and south is 1
    count if inlist(age, 34, 37, 41, 45, 46) & south==1
    
    
    *Attempt to create a local macro
    local x 34 37 41 45 46
    
    *Ideally, I don't want to use the local macro called x to avoid typing out the numbers 34, 37, 41, 45, or 46 over and over again.
    *The lines below don't work. 
    count if inlist(age, x)
    count if inlist(age, x) & race==2
    count if inlist(age, 34, x) & south==1

  • #2
    try
    Code:
     
     local x ="34, 37, 41, 45, 46" count if inlist(age, `x')
    To call a local you need to put it in those special quotes.

    Comment


    • #3
      HI Alejoforero,

      Thanks for the suggestion. The syntax trips me up. Your suggestion helped and addressed my problem. Thank you.

      Comment


      • #4
        @alejoforero

        Is there a way to keep the macro if I run separate chunks of code? For example, I have other datasets with the same variable and I am looking at the individuals with the same age.



        Code:
        *Session 1: Code ran at 12:05 pm
        clear all
        *Call in sample data
        sysuse nlsw88.dta
        
        *Count if someone is either 34, 37, 41, 45, or 46
        count if inlist(age, 34, 37, 41, 45, 46)
        *Count if someone is either 34, 37, 41, 45, or 46 and race is 2
        count if inlist(age, 34, 37, 41, 45, 46) & race==2
        *Count if someone is either 34, 37, 41, 45, or 46 and south is 1
        count if inlist(age, 34, 37, 41, 45, 46) & south==1
        
        *Create a local macro
        local x ="34, 37, 41, 45, 46"
        keep if inlist(age, `x')
        
        
        *Wait five minutes
        
        
        *Session 2: Code ran at 12:10 pm
        clear all
        sysuse nlsw88.dta
        keep if inlist(age, `x')

        Comment


        • #5
          When you are executing the different bits of your code, do so using the "Execute (include)" option available from the top right of your do-file editor window.

          Click image for larger version

Name:	Screenshot 2022-09-24 at 12.58.51 AM.png
Views:	1
Size:	791.7 KB
ID:	1683124

          Comment


          • #6
            Hemanshu Kumar
            Thank you so much. This solves my problem perfectly. I would never have figured it out. Thank you.

            Comment

            Working...
            X