Announcement

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

  • Problems with the command global

    Hi everyone! I have a problem. I'm working with many databases, and I'm using this command to work with all of them:

    clear all
    global work1 "C:\Users\56966\Downloads\Work 1 Labour Economics HenrĂ­quez and Pavisic"
    cd "$work1"

    When I run the command, the part where the database is output is empty (it has no data). Because it can be?

  • #2
    I am not sure what you meant by this:

    the part where the database is output is empty (it has no data). Because it can be?
    but I didn't see any grammatical error. Also, you can check if the cd command worked by following with a pwd (print working directory) after it to see if it's correctly changed.

    And if the directory is correct, then the reason why there is no database is probably not because of the global variable, but in file management or the codes that come after this.

    Comment


    • #3
      I didn't explain myself well. I meant that, when I look at the icon where the databases appear (I upload a photo), it appears empty. Then I use this code because I'm working with national employment surveys, but it keeps coming up empty. I use this commands:

      foreach year in "2018" "2019" "2020" "2021" "2022" "2023" {
      foreach i in "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" {
      foreach j in "def" "efm" "mam" "amj" "mmj" "jja" "jas" "aso" "son" "ond" "nde" {
      capture confirm file ene- `year' - `i' - `j'.dta
      if _rc==0{
      use ene- `year' - `i' - `j'.dta, clear
      gen mes= `i'
      gen agno= `year'
      gen fecha=ym(agno, mes)
      format fecha %tm

      keep agno mes fecha region tipo edad sexo nivel proveedor activ cae_general categoria_ocupacion ocup_form r_p_rev4cl_caenes sector fact_cal ocup_form habituales c10 c11 b7a_1 b7a_2 b7a_3 b7b_1 b7b_2 b7b_3 b7b_4 b8 b9 i4
      keep if (categoria_ocupacion==1 | categoria_ocupacion==2 1 categoria_ocupacion==3)
      }
      }
      }
      }

      "ene" is the name of the survey, "mes" is month and "agno" is year
      Click image for larger version

Name:	Captura de pantalla 2023-09-12 214059.png
Views:	1
Size:	61.6 KB
ID:	1726945
      Click image for larger version

Name:	Captura de pantalla 2023-09-12 214143.png
Views:	1
Size:	74.5 KB
ID:	1726946

      Comment


      • #4
        I haven't read through the entire block of code you showed, but there is a clear problem early on:

        Code:
        capture confirm file ene- `year' - `i' - `j'.dta
        is wrong, because you have put spaces in the filename that don't belong there. It should be:
        Code:
        capture confirm file ene-`year'-`i'-`j'.dta
        Because you put those spaces in your -confirm file- command, Stata looks for files with names like ene- 2017 - 01 def.dta. But there is no such file. There is ene-2017-0-def.dta, but that's not a match to ene- 2017 - 0 - def.dta. So c(rc) is never 0, and the loop ends up doing nothing at all except rejecting all of the incorrect filenames.

        Get rid of those spaces and I think everything will run.

        Comment


        • #5
          Clyde identified the problem. I wish to add a more general point.


          Originally posted by Clyde Schechter View Post
          Because you put those spaces in your -confirm file- command, Stata looks for files with names like ene- 2017 - 01 def.dta.
          Actually, Stata does not even look for any file here:

          Code:
          . confirm file foo bar
          'bar' found where nothing expected
          r(7);
          Note that confirm file expects one argument and receives two (or more). Note that the return code is 7. Compare this to

          Code:
          . confirm file foobar
          file foobar not found
          r(601);

          where Stata does look for a file, cannot find it, and returns 601.

          My more general point is about capture. Whenever possible, use capture noisily and/or explicitly reference expected return codes. The code in question would have been better written as

          Code:
          capture noisily confirm file ene-`year'-`i'-`j'.dta
          if (_rc == 0) {
              (code omitted)
          }
          else if (_rc != 601) exit _rc

          Comment


          • #6
            Hi guys! I corrected the error, so now the code runs. But I have a problem. I use the code to take all the years (2018, 2019, 2020, etc.), and all the months, but when I put the code, Stata only takes the year 2023 and takes month 5. Do you know why this could be happening? I took a picture. Thanks for all the help
            Click image for larger version

Name:	Captura de pantalla 2023-09-13 111338.png
Views:	1
Size:	158.0 KB
ID:	1727006

            Comment

            Working...
            X