Announcement

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

  • if else conditional of number of elements in a local

    Hi,

    I am trying to set a condition within a nested loop. Now, the loop opens some folders; the condition is that if a folder has less than 50 files, than the iteration of the loop should be run. If it has more than 50 files, the iteration should be skipped. I tried to use -if- -else- and -continue- but I am doing something wrong: when a folder has >50 files, the loop is exited with no error produced.
    It is the first time I find myself having to use -if- this way, apologies if the question is trivial.

    Code:
    clear
    local com_list `"`r(folders)'"'
    local years 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
    foreach c of local com_list{
    foreach y of local years{
            clear
            cap n cd "C:/bilanci_unzip/`c'/csv/`c'/`y'"
            fs *
    *if elements of `r(files)' are >50, skip append loop
    local count_values_num: word count `com_list'
    di "Number of numeric values: `count_values_num'"
    
    if `count_values_num'<50 {
    foreach f in `r(files)'{
                append using `"`f'"'
                *this is for having all the right info in labels3
                replace labels3 = labels2 if labels3==""
                }   //close 'files' loop
        *save one file for each comune
        cd "C:/bilanci_unzip/`c'/csv/`c'/"
        save "`c'_`y'", replace
            }
    else{
        continue
        }
    }  //close 'year' loop
    }  //close 'comune' loop
    log close
    Thank you

  • #2
    Every programmer needs personal rules that they try to follow consistently. Indenting is one area of concern. I used to be an 8 character tab person, but now I go for 4.

    That said I don't fully understand what you are trying to do, but note

    1. fs is from SSC, as you are asked to explain (FAQ Advice #12).

    2. Your comment says that you are counting the results of fs, but the code doesn't do it.

    This code simplifies yours and uses indents more consistently. It may help.


    Code:
    clear
    
    local com_list `"`r(folders)'"'
    foreach c of local com_list {
        forval y = 2004/2014 {
            clear
            cap n cd "C:/bilanci_unzip/`c'/csv/`c'/`y'"
            fs *
            * if elements of `r(files)' are >50, skip append loop
            local count_values_num: word count `r(files)'
            di "Number of numeric values: `count_values_num'"
    
            if `count_values_num' < 50 {
                foreach f in `r(files)' {
                     append using `"`f'"'
                     replace labels3 = labels2 if labels3==""
                }  
        
                cd "C:/bilanci_unzip/`c'/csv/`c'/"
                save "`c'_`y'", replace
            }
        }  
    }
    Last edited by Nick Cox; 20 Aug 2019, 04:35.

    Comment


    • #3
      Point #2 was exactly the issue. Thank you for spotting that, it was just a typo but I was fixated that the issue was more fundamental. Thank you for the programming advice too, I have tidied up my code now.

      Comment

      Working...
      X