Announcement

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

  • Making an iterated list look pretty

    This may seem like a super odd question, but I wanna program in-time placebos for a command I've written. Here, we have a base year (say 1990), and we iteratively move the treatment back by however much the user wants.


    Luckily though, I got the estimation covered. I'm more concerned about presentation. I want to keep users abreast of what time period is being used. At present, the
    Code:
    cls
    
    loc interdate = 1990
    
    
    loc times 2 3 4
    
    foreach x of num 0 `times' {
    
    loc npp = `interdate'-`x'
    if `x' > 0 {
    noi di "`npp' (`x' pre-periods)"
    }
    }
    returns the result
    Code:
    1988 (2 pre-periods)
    1987 (3 pre-periods)
    1986 (4 pre-periods)
    Which is cool, but what if the user decided to go nuts and have 25 specific placebo-time interventions? then, we'd have
    Code:
    cls
    
    loc interdate = 1990
    
    
    loc times 1/25
    
    foreach x of num 0 `times' {
    
    loc npp = `interdate'-`x'
    if `x' > 0 {
    noi di "`npp' (`x' pre-periods)"
    }
    }
    Which to me is unappealing. Here's what I want:

    I want the resulting code to read something like
    Code:
    1988 (2 pre-periods)...1987 (3 pre-periods)...1986 (4 pre-periods)
    at the end of each iteration so that the resulting output doesn't take up lots of room. How might I do this?


    This of course is a simplified example, but I imagine I'll be able to adapt it to my purposes.

  • #2
    You can collate the iterations in a local and display when some wanted number of times is reached. However, you need to include exceptions if the iterations are fewer than this wanted number of times and the last iteration as well. Standard if-else coding, but there could be better ways. I do not know how bootstrap does it with the dots!

    Code:
    loc interdate = 1990
    loc times 2 3 4 5 6 7 8 9 10
    
    foreach x of num 0 `times' {
        loc npp = `interdate'-`x'
        if `x' > 0 {
            local pair "`npp' (`x' pre-periods)"
            local disp "`disp' `pair'..."
            if wordcount("`disp'")==9{
                noi di "`disp'"
                local disp ""
            }
       }
    }
    Res.:

    Code:
    . foreach x of num 0 `times' {
      2.
    .     loc npp = `interdate'-`x'
      3.
    .     if `x' > 0 {
      4.
    .         local pair "`npp' (`x' pre-periods)"
      5.
    .         local disp "`disp' `pair'..."
      6.
    .         if wordcount("`disp'")==9{
      7.
    .             noi di "`disp'"
      8.
    .             local disp ""
      9.
    .         }
     10.
    .    }
     11.
    . }
     1988 (2 pre-periods)... 1987 (3 pre-periods)... 1986 (4 pre-periods)...
     1985 (5 pre-periods)... 1984 (6 pre-periods)... 1983 (7 pre-periods)...
     1982 (8 pre-periods)... 1981 (9 pre-periods)... 1980 (10 pre-periods)...
    Last edited by Andrew Musau; 16 Aug 2022, 08:00.

    Comment


    • #3
      Code:
      . loc interdate = 1990
      
      . 
      . 
      . loc times 2 3 4
      
      . loc gap
      
      . 
      . foreach x of num 0 `times' {
        2. 
      . loc npp = `interdate'-`x'
        3. if `x' > 0 {
        4. noi di "`gap'`npp' (`x' pre-periods)" _continue
        5. loc gap ...
        6. }
        7. }
      1988 (2 pre-periods)...1987 (3 pre-periods)...1986 (4 pre-periods)
      .

      Comment


      • #4
        You just need some judicious use of _continue (next display command's text will begin on the same line) and _n (ensures next text begins on a new line) in your display commands.

        Comment


        • #5
          #3 & #4... nice!

          Comment

          Working...
          X