Announcement

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

  • replacing values of a new variable with valid values of other variables

    Hi,
    I would like to generate a new variable and fill the new variable with the valid values from a list of variables named as exceeds_TLE_*. In fact, all the valid values in the exceeds_TLE_* have a diagonal form and can be combined in a single variable. Below please see what I tried.
    Thanks.
    Nader

    local `TLE_exceed' exceeds_TLE_*
    gen TLE_combined=.
    replace TLE_combined=`TLE_exceed' if `TLE_exceed'~=. & TLE_combined==.


  • #2
    Try to think like the Stata parser. Your first command contains a reference to local macro TLE_exceed, which is not defined (at least not in the code you show). So that translates as a null string and Stata reads that command as:

    Code:
    local exceeds_TLE_*
    which tells Stata to create a new local macro named exceeds_TLE_* and set its value to empty string.

    Your next line creates a new variable TLE_combined, with all missing values.

    Your last command, after the reference `TLE_exceed' is replaced by the empty string reads
    Code:
    replace TLE_combined = if ~=. & TLE_combined == .
    which is just a short series of syntax errors.

    Now, I would like to help, but I cannot glean from your explanation what your data are like, nor do I understand what you want to get from them. I have a vague intuition here that you meant for your final replace command to look something like
    Code:
    replace TLE_combined = exceeds_TLE_* if exceeds_TLE* != . & TLE_combined == .
    But that is just a series of deeper syntax errors. The right hand side of the = in -replace- must consist of a single expression: it cannot consist of a list of variables because Stata would have no way of reading your mind as to which of those variables is intended, or perhaps that they should be combined in some unspecified way. Similarly you cannot have exceeds_TLE* != ., because, again, Stata would have no way of knowing which variable in the varlist exceeds_TLE* you are concerned with here or some combination of all of them.

    At 48 posts, I think you have been around this Forum long enough to have been told, or seen others told, that asking for help with code without providing example data, a clear and precise explanation of what you want Stata to do, and, in most cases, some hand-worked results to further clarify what is wanted, is usually a waste of everybody's time. (And, everyone who joins the Forum is asked to read the Forum FAQ before they post, which would have exposed you to similarly phrased advice had you done that.)

    So I suggest you post back with a data example, a clearer explanation of what you want to do, and show what you want the results for the example you show to be. When posting your example data, be sure to use the -dataex- command. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thanks for the clarification. Below please see my dataex code for my current data and what I want.
      *my current data
      clear
      input float(sample_label Age_Group_ meanexceeds_TLE_50_511 meanexceeds_HLE_50_511 meanexceeds_PLE_50_511 meanexceeds_TLE_50_514 meanexceeds_HLE_50_514 meanexceeds_PLE_50_514 meanexceeds_TLE_50_517 meanexceeds_HLE_50_517 meanexceeds_PLE_50_517 missing_only)
      1 1 . . . . . . . . . .
      2 1 935 984 874 . . . . . . .
      3 1 568 331 93 . . . . . . .
      4 1 . . . . . . . . . .
      5 1 . . . 903 975 806 . . . .
      6 1 . . . 467 278 69 . . . .
      7 1 . . . . . . . . . .
      8 1 . . . . . . 870 944 737 .
      9 1 . . . . . . 419 159 48 .
      10 1 . . . . . . . . . .
      end

      *what I want
      clear
      input float(sample_label Age_Group_ meanexceeds_TLE_50_511 meanexceeds_HLE_50_511 meanexceeds_PLE_50_511)
      1 1 . . .
      2 1 935 984 874
      3 1 568 331 93
      4 1 . . .
      5 1 903 975 806
      6 1 467 278 69
      7 1 . . .
      8 1 870 944 737
      9 1 419 159 48
      10 1 . . .
      end

      Comment


      • #4
        OK, thanks, that's clear.

        Now, I may have "outsmarted" myself here. These variable names remind me of a thread you started a few days ago. So I am imagining that you also have variables like meanexceeds_?LE_60_* that you want to handle in the same way, and that in the end results you want to have meanexcdeeds_?LE_50_511, meanexceeds_?LE_60_511, meanexceeds_?LE_70_511 as separate variables. So I have written the code to do that. If I am wrong, post back.

        Code:
        reshape long meanexceeds_TLE_ meanexceeds_HLE_ meanexceeds_PLE_, ///
            i(sample_label Age_Group_) j(seq) string
        drop if missing(meanexceeds_TLE_) & missing(meanexceeds_HLE_) & missing(meanexceeds_PLE_)
        split seq, parse("_") gen(j)
        drop seq j2
        reshape wide meanexceeds_TLE_ meanexceeds_HLE_ meanexceeds_PLE_, ///
            i(sample_label Age_Group) j(j1) string
        rename (meanexceeds*) =_511

        Comment


        • #5
          Thanks. Great solution!
          Last edited by Nader Mehri; 23 Mar 2020, 12:47.

          Comment

          Working...
          X