Announcement

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

  • using /// in lab define

    I am using the lab define command to establish labels for nearly 600 values. To make my do file easier to read I was using something like
    lab define parl_const
    001 " Angus " ///
    002 " Ayr " ///
    003 " Banff and Buchan " ///
    004 " Carrick Cumnock and Doon Valley " ///
    005 " Central Fife " ///
    006 " Clydebank and Milngavie " ///

    I copy pasted from Excel, so I wouldn't have to do all by hand. But I get the same error over and over again: is not a valid command name
    r(199);
    What am I doing wrong?
    Thanks!!!

  • #2
    The first line needs a comment. I'm assuming you are using this in a do-file/do-file editor and not from the command line.

    Code:
        
    lab define parl_const ///
    001 " Angus " ///
    002 " Ayr " ///
    003 " Banff and Buchan " ///
    004 " Carrick Cumnock and Doon Valley " ///
    005 " Central Fife " ///
    006 " Clydebank and Milngavie "
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      I personally prefer to change the line delimiter when I am writing long commands. #delimit ; tells Stata to end the line when it encounters a semicolon. #delimit cr tells Stata to go back to the default behavior of ending a line when it encounters a carriage return (hard return).

      Code:
      #delimit ;    
      lab define parl_const 
      001 " Angus " 
      002 " Ayr " 
      003 " Banff and Buchan " 
      004 " Carrick Cumnock and Doon Valley " 
      005 " Central Fife " 
      006 " Clydebank and Milngavie " 
      ;
      #delimit cr
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        Thanks Carole.
        I was trying to use it in the command line. So it only works if running the do file?
        Thanks! This is all new to me...

        Comment


        • #5
          Yes, comments breaking up a line cannot be used interactively. If you open the do-file editor (type: doedit) and copy and paste your command into that, you can press the button at the top right to run the all the code in the do-file.
          Stata/MP 14.1 (64-bit x86-64)
          Revision 19 May 2016
          Win 8.1

          Comment


          • #6
            Thanks Carole!

            Comment


            • #7
              Something else is wrong. I tried to open it from the do file and it still brings up invalid syntax
              Are you aware of what sort of tabulation is permitted in the do file?
              I couldn't find a help command for this
              Thanks

              Comment


              • #8
                Here is the command in post #2
                Code:
                lab define parl_const ///
                001 " Angus " ///
                002 " Ayr " ///
                003 " Banff and Buchan " ///
                004 " Carrick Cumnock and Doon Valley " ///
                005 " Central Fife " ///
                006 " Clydebank and Milngavie "
                And here is the output from when I run the do-file it is in by clicking the Do button in the upper right corner of the window
                Code:
                . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD01159.000000"
                
                . lab define parl_const ///
                > 001 " Angus " ///
                > 002 " Ayr " ///
                > 003 " Banff and Buchan " ///
                > 004 " Carrick Cumnock and Doon Valley " ///
                > 005 " Central Fife " ///
                > 006 " Clydebank and Milngavie "
                
                . 
                end of do-file
                As you can see, it works for me. You must be doing something other than what I did. Perhaps you can compare what I did with what you did and find your problem. But unless you show us, as I did, what you have in your do-file, how you ran it, and what Stata told you in response, we cannot tell you what you did incorrectly.

                Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

                The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

                Section 12.1 is particularly pertinent

                12.1 What to say about your commands and your problem

                Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!

                Comment


                • #9
                  Orthogonally perhaps, just to mention that while Stata will not object to leading zeros, it will ignore them:


                  Code:
                  . label define foo 001 "whatever"
                  
                  . label li
                  foo:
                             1 whatever
                  More directly, if "001" "002" ... are values of a string variable please note that in Stata you cannot attach value labels to a string variable. Perhaps this is what is failing. Otherwise William is, as usual, bang on: a error report that is just "invalid syntax" could be anything from a trivial typo to a major misunderstanding. You're not giving us either a data example or the exact code that is failing. Please do (re-)read the FAQ Advice and act on it.
                  Last edited by Nick Cox; 21 Aug 2018, 10:05.

                  Comment


                  • #10
                    Hi William,

                    this doesn't run from the command box, does it? I have to open the do file from the database window. As I want to check the syntax line by line before building a long do file that doesn't work, I am using the command.

                    Thanks Nick, I wasn't aware of that. It saves me a lot of time, as most of my variables, have values such as 001, 002... etc.

                    Comment


                    • #11
                      In post #4 you wrote

                      I was trying to use it in the command line. So it only works if running the do file?
                      and in post #5 Carole confirmed that it only works if run from a do-file, not in the command line.

                      In post #7 you wrote

                      I tried to open it from the do file and it still brings up invalid syntax
                      without saying what "it" was.

                      In post #8 I assumed that "it" was the corrected command Carole showed in post #2 and demonstrated that it worked when run from a do-file, and suggested improving your presentation to make it clearer what you did and what Stata responded. I wonder now if perhaps "it" was the uncorrected command you showed in post #1.

                      In post #10 you wrote

                      this doesn't run from the command box, does it
                      which has nothing to do with what my demonstration in post #8 that the command in post #2 would indeed work from a do-file, and which Carole already addressed in post #4.

                      My advice from post #8 that you review the Statalist FAQ especially sections 9-12 on how to best pose your question still stands.

                      Comment


                      • #12
                        Thank you for bringing to my attention that my post was not intelligible, I will try to be clearer.
                        As /// does not work with command lines, I eventually gave up on it and replaced all /// by Carol's suggestion- also, I had a typo on the first line of the syntax, as Nick detected.
                        I get the following message when I am running the do file:

                        Code:
                        > recode soft (3=2) (8=3) (5=15) (2=5) (04 6 7=8) ,gen (soft_h)
                        > #delimit ;
                        invalid 'gen'
                        My code is:
                        Code:
                        recode soft (3=2) (8=3) (5=15) (2=5) (04 6 7=8) ,gen (soft_h)
                        #delimit ;
                        lab define soft_h 1 "Colour"
                        2 "Lemon"
                        3 "Water"
                        4 "Orange"
                        5 "Tan"
                        6 "Other1"
                        7 "Other2"
                        8 "Other3"
                        9 "Don't Know"
                        ;
                        #delimit cr
                        The first line of the syntax recode soft (3=2) (8=3) (5=15) (2=5) (04 6 7=8) ,gen (soft_h) does run in the command box, when I try it.
                        Hope this is clearer?
                        Thank you very much
                        Last edited by Rita Neves; 31 Aug 2018, 09:30.

                        Comment


                        • #13
                          Your code works for me when run from a do-file. But when you look at the output below, and compare it to what you show above, it appears that your recode command and the following #delimit command were interpreted by Stata as a continuation of whatever appeared before it in the do-file. You show
                          Code:
                          > recode soft (3=2) (8=3) (5=15) (2=5) (04 6 7=8) ,gen (soft_h)
                          > #delimit ;
                          Stata uses ">" to display a continuation line, and "." (as in the example below) to display the first line of a command.
                          Code:
                          // fake some input data
                          sysuse auto, clear
                          rename rep78 soft
                          // your code beins here
                          recode soft (3=2) (8=3) (5=15) (2=5) (04 6 7=8) ,gen (soft_h)
                          #delimit ;
                          lab define soft_h 1 "Colour"
                          2 "Lemon"
                          3 "Water"
                          4 "Orange"
                          5 "Tan"
                          6 "Other1"
                          7 "Other2"
                          8 "Other3"
                          9 "Don't Know"
                          ;
                          #delimit cr
                          Code:
                          . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD02943.000000"
                          
                          . // fake some input data
                          . sysuse auto, clear
                          (1978 Automobile Data)
                          
                          . rename rep78 soft
                          
                          . // your code beins here
                          . recode soft (3=2) (8=3) (5=15) (2=5) (04 6 7=8) ,gen (soft_h)
                          (67 differences between soft and soft_h)
                          
                          . #delimit ;
                          delimiter now ;
                          . lab define soft_h 1 "Colour"
                          > 2 "Lemon"
                          > 3 "Water"
                          > 4 "Orange"
                          > 5 "Tan"
                          > 6 "Other1"
                          > 7 "Other2"
                          > 8 "Other3"
                          > 9 "Don't Know"
                          > ;
                          
                          . #delimit cr
                          delimiter now cr
                          .
                          end of do-file

                          Comment


                          • #14
                            Thanks William, that is really useful
                            Do you have any idea why this happens with my code and how it can be solved? -I did run it from both the command and the do file-none works)

                            Comment


                            • #15
                              In post #12 you wrote

                              The first line of the syntax recode soft (3=2) (8=3) (5=15) (2=5) (04 6 7=8) ,gen (soft_h) does run in the command box, when I try it
                              In post #14 you wrote

                              I did run it from both the command and the do file-none works
                              Those seem to contradict each other, so I have no idea what you are asking about in post #14.

                              Let me add that in post #13, I thought it was obvious that if Stata is treating the two lines as continuation lines, then the line before it must end in a continuation mark /// or else you included #delimit ; at some earlier point in the do-file, and neglected to use #delimit cr to return it to normal. But since you only showed the code you think caused your error, rather than including what you had done before that point, there's not much more I can say about what you did wrong.

                              Comment

                              Working...
                              X