Announcement

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

  • Using macros to define directories

    Hi everyone,

    I am trying to define directories in a do file . Our team uses Box Sync to share work folders, and my code to define file paths is as follows:


    local corredor=c(username)

    if "`corredor'"=="jasle" {
    global data="C:\Users\jasle\Box Sync\PMMVY Project\Data"
    }

    if "`corredor'"=="mcb76" {
    global data="C:\Users\mcb76\Box Sync\PMMVY Project\Data"
    }


    global input= "$data\data_raw"
    global output="$data\output"
    global do = "$data\do_files"

    *defining the dataset
    use "$input\scoping\listing\listing_raw_15-10-2018.dta"



    But this gives me an error: file \data_raw\scoping\listing\listing_raw_15-10-2018.dta not found, which probably means that it is not picking up anything from global data.

    The above system works perfectly when I used it on Dropbox for a different work team. May I know what's going wrong here?

    Thanks!
    Last edited by Mantasha Husain; 21 Oct 2018, 09:16. Reason: global ma

  • #2
    Just before your use statement type the command

    Code:
    macro list
    to see what Stata thinks are your defined global and local macros.

    Comment


    • #3
      Hi Nick,

      This is what I typed (I have added another user), but the macro list picks up the first username on my computer, whereas, the file paths on my computer display the third username (C:\Users\jasle\Box Sync\PMMVY Project\Data\do_files\scoping\listing). Someone else used my computer before and set it up with their name (username jasle), whereas the macros pickup the username Mantasha Husain, and say that the file was not found. Is this a system issue, and not a Stata one?

      local corredor=c(username)

      .
      . if "`corredor'"=="Mantasha Husain" {
      . global data="C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data"
      . }

      .
      . if "`corredor'"=="mcb76" {
      . global data="C:\Users\mcb76\Box Sync\PMMVY Project\Data"
      . }

      .
      . if "`corredor'"=="jasle" {
      . global data="C:\Users\jasle\Box Sync\PMMVY Project\Data"
      . }

      .
      . global input= "$data\data_raw"

      . global output="$data\output"

      . global do = "$data\do_files"

      .
      .
      . macro list
      do: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\do_files
      output: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\output
      input: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\data_raw
      data: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data
      S_level: 95
      F1: help advice;
      F2: describe;
      F7: save
      F8: use
      S_ADO: BASE;SITE;.;PERSONAL;PLUS;OLDPLACE
      S_StataSE: SE
      S_FLAVOR: Intercooled
      S_OS: Windows
      S_OSDTL: 64-bit
      S_MACH: PC (64-bit x86-64)
      _corredor: Mantasha Husain

      . use "$input\scoping\listing\listing_raw_15-10-2018.dta"
      file C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\data_raw\scoping\listing\listing_raw_ 15-10-2018.dta
      not found

      Comment


      • #4
        In your report there is a space in one instance, not in the other. Look again at

        Code:
        \listing_raw_15-10-2018.dta
        \listing_raw_ 15-10-2018.dta

        Comment


        • #5
          Yeah, but that's not an issue. Must be a typo when I pasted the thing here. This is what it looks like:

          . local corredor=c(username)

          .
          .
          . if "`corredor'"=="Mantasha Husain" {
          . global data="C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data"
          . }

          .
          .
          . if "`corredor'"=="mcb76" {
          . global data="C:\Users\mcb76\Box Sync\PMMVY Project\Data"
          . }

          .
          .
          . if "`corredor'"=="jasle" {
          . global data="C:\Users\jasle\Box Sync\PMMVY Project\Data"
          . }

          .
          .
          . global input= "$data\data_raw"

          . global output="$data\output"

          . global do = "$data\do_files"

          .
          .
          .
          . macro list
          do: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\do_files
          output: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\output
          input: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\data_raw
          data: C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data
          S_level: 95
          F1: help advice;
          F2: describe;
          F7: save
          F8: use
          S_ADO: BASE;SITE;.;PERSONAL;PLUS;OLDPLACE
          S_StataSE: SE
          S_FLAVOR: Intercooled
          S_OS: Windows
          S_OSDTL: 64-bit
          S_MACH: PC (64-bit x86-64)
          _corredor: Mantasha Husain

          . use "$input\scoping\listing\listing_raw_15-10-2018.dta"
          file C:\Users\Mantasha Husain\Box Sync\PMMVY Project\Data\data_raw\scoping\listing\listing_raw_ 15-10-2018.dta
          not found




          The file is in this folder on my laptop: C:\Users\jasle\Box Sync\PMMVY Project\Data\data_raw\scoping\listing

          I think this is a system issue, don't know how to sort it out with Stata though

          Comment


          • #6
            Sorry, but I can see nothing wrong with your Stata syntax and necessarily know nothing about your files or set-up generally.

            Comment


            • #7
              No worries, thanks a lot Nick!

              Comment


              • #8
                I would start debugging this by just working interactively in Stata. Start with just trying to cd, in Stata, to the directory where the file is stored and see if you get an error along the way:

                Code:
                cd C:
                cd \Users
                cd jasle
                cd "Box Sync"
                cd "PMMVY Project"
                cd Data
                cd data_raw
                cd scoping
                cd listing
                Assuming that works, now see if you can see any files within that directory in Stata:

                Code:
                dir
                Assuming you see the file there, now try to use it

                Code:
                use listing_raw_15-10-2018.dta

                The error you reported in your first post above makes it look like global data was never defined, but your later posts make it look like Stata simply can't find the directory or the file within the directory. Hopefully the steps above will help you find what is going wrong.

                Also check permissions on the file of interest.

                Comment

                Working...
                X