Announcement

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

  • ADO project doesn't apply restriction

    Hello,
    I'm trying to write a simple ADO.
    The idea is to write:
    simple2 var1 var2 in order to create something as:
    table var1, c(sum var2 count var2).
    Always It was a goal to show messages if the user declared 1 or 3/4/5/...+ variables.

    This is what I have:
    HTML Code:
    capture program drop simple2
    program simple2
    
    local variables `1' `2'
    
    local elements_v: word count `variables'
    
    
    if `elements_v'==2 {
        
    table  `1', c(sum `2'  count `2')
        
    }  
    else if `elements_v'=>3 {
    
    display "You are using too many variables"
    
    }
    else if inrange(`elements_v',0,1) {
    
    display "You are missing a variable/weight"
    
    }
    else {
    
    }
    
    
    
    The code inform you the weighted an unweighted table using the v1 as variable to tabulate, with v2 as iweight.
    The code works if I declare 1 or 2 variables. The messages displays ok using only 1 variable on simple2.
    However, If I declare 3 or more variables on simple2, I don't receive the alert. Simple2 just performs the following syntax:
    table var1 [iw=var2]
    Every variable after var2 doesn't matter to simple2.
    The error comes from the count of elements_v.
    But I can't fix it.
    Can you guide me, please?
    Thanks for your help and time.



  • #2
    We encourage and appreciate the use of full real names here on Statalist. This is stated in the FAQ. The table command does not allow the option c() [short for contents()] as of Stata 17. If you are using an older version of Stata, you are supposed to tell us. This is stated in the FAQ.

    As for your question, the line

    Code:
    local variables `1' `2'
    stores only the first two arguments into the local macro variables. Thus, unless the user encloses arguments in double quotes, there can only be a maximum of two variable names inside the local variables. The short solution to your problem is

    Code:
    capture program drop simple2
    program simple2
        version 16
        
        syntax varlist(max=2)
        
        gettoken varname1 varname2 : varlist
        
        table  `varname1', c(sum `varname2'  count `varname2')
    end
    Here, I made syntax check that the number of variables is correct; if it is not, syntax will throw the appropriate error. Note that syntax will be perfectly happy with specifying the same variable twice; you might want to catch that.

    Note also that I have specified version 16 near the top of the program so Stata 17 will not throw errors because of the changed syntax for the table command.
    Last edited by daniel klein; 21 Jul 2022, 21:41.

    Comment


    • #3
      You may revise the third line of code to

      Code:
      local variables `0'
      as `1' `2' tell Stata to pick the first two words only no matter how many variables you actually type.

      Also there is a typo

      Code:
      else if `elements_v' >= 3

      Comment


      • #4
        A tiny revision to Daniel's helpful solution in #2:

        Code:
        syntax varlist(max=2 min=2)
        so that a proper error message will be displayed if too few variables are specified.

        Comment


        • #5
          Fei Wang is right; good catch. No need to wait for table to complain when the local varname2 is empty. In fact, if the dataset happened to have a variable called count, which is perfectly legal, my code would produce misleading results.
          Last edited by daniel klein; 21 Jul 2022, 21:59.

          Comment


          • #6
            Thank you all.
            I learned a lot from your replies.
            Thanks again, people.

            Comment

            Working...
            X