Announcement

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

  • Looping through a list of strings; combining variables within strings

    Hi everyone,
    I have a series of household consumption datasets, each for every year, and I want to process each file a bit before merging them with other files.
    I have two issues (examples follow bellow):
    1. "use" command: it works when I explicitly state the file name, but not when I try to include a local variable in the path.
    2. looping through a list of strings: this might be due to issue 1#, but I can't manage to define a list of strings - or an "an array of strings" in other programing languages - and loop through them.

    The relevant do file goes as follows:
    Code:
    clear
    set more off
     
    local hshall f477fam_1997 f478fam_1998 f479fam_1999 f461fam_2000 f462fam_2001 f463fam_2002 f464fam_2003 f466fam_2004 f467fam_2005 n468fam_2006 f469fam_2007 f474fam_2008 f472fam_2009
    
    foreach filename of local hshall {
     display `filename'
     use "${data}raw\households\`filename'.dta", clear //global "data" variable defined in the master do file
    
     if yearsur<2003 { //change variable names for 97'-02' data
      rename v1 rectype
      rename v2 hhnum
      rename v15 weight
      rename v13 typeloc
      }
        
     keep yearsur rectype hhnum weight typeloc
     save "${data}\inter\hsh\hsh`yearsur'.dta", clear
    }
    To emphasize issue 1#, the following code didn't work (I got r(111): "f477fam_1997 not found") but using the explicit file path "${data}raw\households\f477fam_1997.dta" does work.
    (I tried putting hshall values in qoutes but it didn't work as well..)

    This issue has agonized me for long time, although it honestly seems so trivial. I tried substantive googling but to no avail.
    Thanks a lot!
    Last edited by Yuval Rittberg; 17 May 2019, 12:22.

  • #2
    Using a backslash before a local macro reference is biting you. See e.g.

    [U] 18.3.11 Constructing Windows filenames by using macros

    Stata uses the \ character to tell its parser not to expand macros.

    Windows uses the \ character as the directory path separator.

    Mostly, there is no problem using a \ in a filename. However, if you are writing a program that
    contains a Windows path in macro path and a filename in fname, do not assemble the final result as

    `path'\`fname'

    because Stata will interpret the \ as an instruction to not expand `fname'. Instead, assemble the
    final result as

    `path'/`fname'

    Stata understands / as a directory separator on all platforms.
    SJ-8-3 pr0042 . . . . . . . Stata tip 65: Beware the backstabbing backslash
    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
    Q3/08 SJ 8(3):446--447 (no commands)
    tip to use forward slash instead of backslash for
    directory and file names
    https://journals.sagepub.com/doi/pdf...867X0800800310

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Using a backslash before a local macro reference is biting you. See e.g.



      SJ-8-3 pr0042 . . . . . . . Stata tip 65: Beware the backstabbing backslash
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q3/08 SJ 8(3):446--447 (no commands)
      tip to use forward slash instead of backslash for
      directory and file names
      https://journals.sagepub.com/doi/pdf...867X0800800310
      You're my hero. Thanks a lot!

      Comment


      • #4
        Yuval Rittberg , Nick Cox is everybody's hero :-) (link) :
        Click image for larger version

Name:	Nick Cox - civilian superhero.png
Views:	1
Size:	47.4 KB
ID:	1499176



        Comment

        Working...
        X