Announcement

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

  • Deleting variables with specific values in a specific row

    I would like to keep variables if the third-row value is SHD.
    If the third-row value is not SHD, I would like to delete all the variables.

    I do not want to use the 'xpose 'command because the data is very long, so it is not workable.
    In my dataset, there are more than 2000 variables including more than 100 SHD and more than 30000 observation.
    I tried the below code but failed.
    I would like to use logic similar to the below.
    PLZ let me know good ways.



    foreach var of varlist * {
    drop `var' if `var'[3] == "SHD"
    }

    V119 V534 V1461 V1942 V1943 V2165 V2542 V2543 V2791 V3002 V3046
    1968 1968 1968 1968 1968 1968 1968 1968 1968 1968 1968
    1 2 3 4 5 6 7 8 9 10 11
    SEX FMN SHD AGE SEX SHD AGE SEX SHD 1973 MI_Y
    1 1
    1 49 2 5 50 2 5 81 5179
    1 5 24 1 5 25 1 5 17 8000
    1 2
    1 59 2 5 60 2 5 2285 0
    1 3 5 82 1 5
    1 4 5 48 1 5 49 1 5 430 0
    20 1 1 432 5200


  • #2
    Added in edit:

    I see the problem now. Try
    Code:
    foreach var of varlist * {
    if `var'[3] == "SHD" drop `var'
    }
    The output of help drop gives a hint - drop does not take the if qualifier when dropping variables. You need the programmer's if command described in help ifcmd.

    The advice on what constitutes a helpful post given in my original post below remains good advice.

    Original post:

    The code you show appears correct, so your problem really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. Show your code and what Stata told you by copying the commands and the resulting error message from Stata's Results window and pasting it into a post using code delimiters [CODE] and [/CODE].

    The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output. 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!
    ...
    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
    Last edited by William Lisowski; 24 Feb 2021, 07:16.

    Comment

    Working...
    X