Find & Range

Search your do-file by string or display any block of lines.

Find mode

find() searches your do-file for a word or phrase and displays each match with configurable surrounding context.

Controlling context lines

Use lines(#) to change how many lines appear after each match. Default is 5.

do2screen using "analysis.do", find("Poverty") lines(2)
    do-file:  analysis.do          Open
------------------------------------------------------------------------------------------------

Line |                 Writing code for:   Poverty
-----+------------------------------------------------------------------------------------------
   3: * Search targets: Poverty (3x), WELFARE (1x), normalize (2x), deflate (1x)
   4:
   5:
                                  ---------- (end of section 1 for Poverty) ----------

   5: * ---- Section A: Poverty measurement ----
   6: * Poverty line threshold (1.90 USD PPP per day, annualized)
   7:
                                  ---------- (end of section 2 for Poverty) ----------
do2screen using "analysis.do", find("Poverty") lines(10)

Shows 10 lines of context after each match — useful for seeing the full implementation block.

Searching for multiple terms

To find multiple terms in a single call, wrap them in double compound quotes. Each term is searched independently:

* Find "poverty line" (as a phrase) and also the word "welfare"
do2screen using "analysis.do", find(`" "poverty line" welfare "')
ImportantExact phrases vs. individual words
  • find("poverty line") — finds lines containing “poverty” and lines containing “line” separately
  • find(” “poverty line” “’)` — finds lines containing the exact phrase”poverty line”

To search for phrases containing spaces, you must wrap the phrase in double quotes inside the compound quotes: `" "poverty line" "'

NoteDouble quotes in output

When the text around a match contains double-quoted strings (e.g., label variable x "My label"), the output may show DQDQ in place of ". This is expected — do2screen uses a placeholder system to prevent Stata from interpreting the quotes during processing. See Caveats & Limitations for details.


Range mode

range() displays a specific block of lines without any searching — useful when you already know which section of the do-file you want to inspect.

Two-number range

Provide the start and end line numbers:

do2screen using "analysis.do", range(13 18)
    do-file:  analysis.do          Open
------------------------------------------------------------------------------------------------

Line |                 Writing code between lines:   13 & 18
-----+------------------------------------------------------------------------------------------

  13: gen income = wages + transfers
  14: replace income = 0 if income < 0
  15: replace income = . if missing(wages) | missing(transfers)
  16:
  17: * ---- Step 4: Household aggregation ----
  18: egen hhincome = sum(income), by(hhid)
                                            ---------- (end of analysis of lines between 13 & 18)

One-number range with lines()

Provide a start line and use lines() to indicate how many lines to show. Default is 5:

do2screen using "analysis.do", range(13) lines(5)

Shows lines 13–18 (start line + 5 following lines).

do2screen using "analysis.do", range(13) lines(10)

Shows lines 13–23.


Saving output

Both find() and range() support the same text export options as var():

* Save find results
do2screen using "analysis.do", find("poverty") text("output/poverty_search") replace

* Save range results
do2screen using "analysis.do", range(13 18) text("output/income_block") replace

Return values

After a find() or range() call, the selected lines are stored in:

  • s_varcode scalar — all lines concatenated (name overridable with scalarname())
  • r(nlines) — number of lines selected
  • r(lines) — matrix of selected line numbers
  • _fr_do2screen — frame with columns line, code, variable