Announcement

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

  • organizing a _label define - command in many lines in STATA do file

    Hello everyone

    I am writing a - label define - command in STATA and it is too long. I would like to organize it in the do file such that it is written in many lines and at the same time I can run it either directly from the do file or by copying and pasting it in the command window. Is there any way to do this?

    An example is:

    label define one_digit 1 "Managers" 2 "Professionals" 3 "Technicians and Associate Professionals" 4 "Clerical Support Workers" 5 "Services and Sales Workers" 6 "Skilled Agricultural, Forestry and Fishery Workers" 7 "Craft and Related Trades Workers" 8 "Plant and Machine Operators and Assemblers" 9 "Elementary Occupations"

    Thanks a lot

  • #2
    The immediate answer to your question is to use a ";" delimiter for the command:

    Code:
    #delimit ;
    label define one_digit 1 "Managers"
    2 "Professionals"
    3 "Technicians"
    ...
    9 "Elementary Occupations";
    #delimit cr
    However, that will not work in the command line.

    An alternative approach altogether, which will work in the command line, is to abandon the lengthy label define command, and instead create a new dataset containing your label definitions, which you can subsequently merge with your original dataset, using -labmask- (part of Nick Cox's labutil package) to apply the label:

    Code:
    clear
    input varname str25 label
    1 "Professionals"
    3 "Technicians"
    ...
    9 "Elementary Occupations"
    end
    merge 1:m varname using your_dataset
    labmask varname, values(label)
    Last edited by Ali Atia; 01 Aug 2022, 08:37.

    Comment


    • #3
      The answer is: no. You cannot have a command spanning multiple lines in the command line. The delimit commands do not work in the command line; neither do /// line-continuation comments. Fortunately, neither is necessary.

      Code:
      --- start mylabel.do ---
      label define one_digit ///
          1 "Managers"       ///
          2 "Professionals"  ///
          ...8
          9 "Elementary Occupations"
      --- end mylabel.do ---
      From the command line:

      Code:
      do mylabel.do
      No copy and paste needed.
      Last edited by daniel klein; 01 Aug 2022, 08:40.

      Comment


      • #4
        To address the original question, we can follow the example of the dataex command and define our label as follows.
        Code:
        label define one_digit 1 "Managers", modify
        label define one_digit 2 "Professionals", modify
        label define one_digit 3 "Technicians", modify
        label define one_digit 9 "Elementary Occupations", modify
        label list one_digit
        or we can be a little more cautious and use
        Code:
        capture label drop one_digit
        label define one_digit 1 "Managers", add
        label define one_digit 2 "Professionals", add
        label define one_digit 3 "Technicians", add
        label define one_digit 9 "Elementary Occupations", add
        label list one_digit
        Either of these examples will run from the do-file and will run when copied-and-pasted into the Command window.

        Comment


        • #5
          Many thanks all for your helpful replies

          Comment

          Working...
          X