Announcement

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

  • How to replace first row with null (missing) for all string values

    Hi,

    I am working with a database in which I want to replace the value of all strings in the first row with "" (missing). I tried to run the following code, hoping that stata would identify all strings values. But string values of different lengths do not seem to be considered of type "string". I could not find the same requests in other discussions.
    Any ideas how to correct my code?

    foreach v of varlist id - formcasecase_id {

    local type: type `v'
    display "`v' is of type `type'"
    display "First observation of `v': `=`v'[1]'"

    if "`type'" == "string"{
    replace `v' = "" in 1
    }
    }

    Thank you!

  • #2
    Are any values in the first observation of any use? If not, then


    Code:
    drop in 1
    is the way to deal with this problem. In my experience, a useless first observation is often the result of reading in column headers from worksheets, .csv files and the like whenever they contain metadata, not data.

    Otherwise, Stata will do the segregation for you

    Code:
    ds, has(type string)
    
    foreach v in `r(varlist)' {
           replace `v' = "" in 1
    }
    Recommendation: in a Stata forum, please use Stata terminology. Spreadsheets have rows and columns; Stata has observations and variables.
    Last edited by Nick Cox; 15 Aug 2024, 04:18.

    Comment

    Working...
    X