Announcement

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

  • Copy Variable Label, Generating a New Variable

    Hi, I got a 100 variables, each having a different name but not any sequence in the name. I wants to create another 100 variables by multiplying the 100 variables with a new variable called "ABC". I am successful in doing so easily with a simple loop (command 1) provided on one of the forums.

    Command 1:

    foreach var of varlist Ist variable name - 100th Variable name {
    gen ABC_`var' = ABC*`var'
    }

    The command given above generates 100 variables each having their previous name starting with ABC_. However, the problem is that I also needs the "Variable Label" copied with new 100 variables from previous 100 hundred variables. I tried this with the given command 2.

    Command 2:


    foreach var of varlist Ist variable name - 100th Variable name {
    labgen2 ABC_`var' "ABC_`labels'"= ABC*`var'
    }

    The command 2 provides Variable Label only "ABC_" However I needs the actual variable Label together with "ABC_" e.g. ABC_label1. I wish to get copy the variable label from the actual variable, combine it with ABC_ and copy it automatically with a new generated variable (for all 100 variables).

    Help will be appreciated. Thanks



  • #2
    It's best not to mix pseudocode and real code and to use CODE delimiters to show real code. As I understand it you want something like

    Code:
    foreach var of varlist <varlist> { 
           local lbl : variable label `var' 
           gen ABC_`var' = ABC * `var' 
           label var ABC_`var' "ABC_`lbl'" 
    }
    where naturally <varlist> needs to be filled in with your real list.

    labgen2 is from SSC (labutil2 package), as you are asked to explain. FAQ Advice #12 says more.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      It's best not to mix pseudocode and real code and to use CODE delimiters to show real code. As I understand it you want something like

      Code:
      foreach var of varlist <varlist> {
      local lbl : variable label `var'
      gen ABC_`var' = ABC * `var'
      label var ABC_`var' "ABC_`lbl'"
      }
      where naturally <varlist> needs to be filled in with your real list.

      labgen2 is from SSC (labutil2 package), as you are asked to explain. FAQ Advice #12 says more.
      Thanks Nick, It worked perfectly. I was expecting the solution from you. You are right, I should have clarified that labgen2 is from SSC. Thanks and have a wonderful day.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        It's best not to mix pseudocode and real code and to use CODE delimiters to show real code. As I understand it you want something like

        Code:
        foreach var of varlist <varlist> {
        local lbl : variable label `var'
        gen ABC_`var' = ABC * `var'
        label var ABC_`var' "ABC_`lbl'"
        }
        where naturally <varlist> needs to be filled in with your real list.

        labgen2 is from SSC (labutil2 package), as you are asked to explain. FAQ Advice #12 says more.
        Could you explain why did you add "*" after ABC? I don't understand this symbol.

        Comment


        • #5
          Multiplication

          Comment

          Working...
          X