Announcement

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

  • Varlist required error

    Hello everyone,

    This might seem a very simplistic problem, but I am getting an error "varlist required" when I try to run a loop to clean variables with different names but with similar entry issues. I feel I have specified the varlist though. Here is the code below:


    program define clean_var, rclass

    version 13.1
    syntax varlist

    disp "Variable List: `varlist'"

    foreach var in `varlist' {
    disp "Looping through: `var'"

    ***** SUBSTR FIXES *****
    replace `var' = trim(itrim(`var'))
    replace `var' = subinstr(`var', char(10), "", .)
    replace `var' = subinstr(`var', char(13), "", .)


    ***** CHARACTER FIXES *****
    replace `var' = "" if `var' == "-"
    replace `var' = "" if `var' == "`"
    replace `var' = "" if `var' == ","


    ***** DATE FIXES ******
    replace `var' = "1/2" if `var' == "02jan2003"
    replace `var' = "1/2" if `var' == "02jan2003"
    replace `var' = "1/2" if `var' == "02jan2019"

    }

    Any help would be really appreciated!

    Thank you,
    Ayon

  • #2
    Hi Ayon,

    Your code is incomplete, and furthermore, should be wrapped in CODE tags to enhance readability.

    I don't see why, in this case, you need to define your loop within a macro program. The following loop should be sufficient, noting that the syntax of the -foreach- loop can be fixed.

    Code:
    foreach var of varlist `varlist' {
       disp "Looping through: `var'"
       // do cleaning steps
    }
    If you did want to wrap this loop in a program, then that's fine, but you will need to still fix the foreach loop, and end the program. Then you can call the program using -clean_var varlist-.

    Comment


    • #3
      Leonardo Guizzetti I agree with your general remarks. Commenting in detail on your suggestion in red: This suggestion is harmless but makes no real difference. In essence

      Code:
      syntax varlist
      ensures that (1) anything not a varlist will halt the program with an error message (2) the varlist produced for later use within the program is a list of single names, so wildcards or variable ranges are all expanded.

      In short, the benefits of

      Code:
      foreach ... of varlist ...
      are already benefits ensured by syntax. Here is a demonstration -- relying on the quirk that syntax looks at the content of local macro 0 so that we can see how it works without defining a program. .


      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . local 0 m*
      
      . syntax varlist
      
      . di "`varlist'"
      make mpg
      
      . local 0 m* frog toad
      
      . syntax varlist
      variable frog not found
      r(111);

      This still leaves the question of why the program in #1 fails. I can see that the program will fail if fed any numeric variable and I am puzzled that it is r-class when no r-class results are visibly defined, but the first bug is not biting and the second fact is not illegal.

      My best guess is that Ayon is trying to run the program one line at a time, in which local macro definitions will not visible after they are executed.
      Last edited by Nick Cox; 17 Jul 2020, 08:39.

      Comment


      • #4
        Thanks, Nick. You are of course correct about for two equivalent loops.

        Comment


        • #5
          I actually figured out the problem myself later and it was quite a silly, beginner's error! But still, thank you for your comments Leonardo and Nick.

          Comment

          Working...
          X