Announcement

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

  • Subinstr function not working in STATA 17

    I am looping 10 csv files (which are monthly data) and then trying to generate a month variable as a unique identifier. I tried to use the subinstr function to extract the month value from the file name but the function doesn't work. However, when I type "help subinstr" there is an explanation. Has stata changed the function name? How can I get around the issue. My code is below. X represents 01_2021, 02_2021, and so on all the way to 10_22. I am trying to extract the value "01,02, 03, etc. and make that the month value for the respective csv file.

    HTML Code:
    foreach x of local files {
            di "`x'" // Display file name
            
            *Import each filepath
            qui: import delimited "`x'", delimiter (",") case (lower) clear // Import csv file
            qui : gen id = subinstr ("`x'", "_2021.csv", "", .) // Generate id variable(same as file name but     without _2021.csv)
            
            *Append each file to masterfile
            append using `dataset', force
            save `dataset', replace
            
    }
    This is the error I get:
    subinstr not found
    r(111);
    Last edited by Melat Kassa; 17 Nov 2021, 13:36.

  • #2
    Remove the space between the function name subinstr and the left parenthesis following it.
    Code:
    . generate x = sqrt (4)
    sqrt not found
    r(111);
    
    . generate x=sqrt(4)
    
    .
    Added in edit: while spaces are allowed between an option name and its arguments, as your import command demonstrates, they are not allowed between a function name and its arguments, because "sqrt" could be a variable name or a function name, and some way is needed to tell the two apart.
    Last edited by William Lisowski; 17 Nov 2021, 14:24.

    Comment


    • #3
      Thank you, William! I didn't know functions behaved differently when it came to spaces.

      Comment

      Working...
      X