Announcement

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

  • Shifting existing data one column over to the left

    Hi,

    Below is a screenshot of my data (still in csv format). The highlighted rows (in yellow) are the right format. Due to a data entry error in the un-highlighted rows, the SYSTEMID column has incorrect data entered. To correct this, I need all data in the variables to the right of SYSTEMID moved over one to the left for the un-highlighted columns. If I write an "if" statement to identify observations with incorrect data entered, is there a command that will help shift all the data over to the left by one column from all variables to the right of SYSTEMID?

    Thanks,
    karishma

    Click image for larger version

Name:	Amber.png
Views:	1
Size:	14.1 KB
ID:	1582179

  • #2
    You refer to "un-highlighted columns" but show highlighting of rows, which is confusing. I'll presume you mean rows.

    Had you supplied a data example using -dataex-, as described in the StataList, giving you a more or less exact answer and testing it would have been possible, but without it, about all that can be done is to sketch the outline of what you could do.

    I'd advise you to import the CSV file with all variables treated as strings, and do something like this:
    Code:
    replace service_description = service_description + systemid if BadObservation
    unab vlist: systemid-WhateverYourLastVariableIs
    local nvar: word count `vlist'
    forval i = 1/`=`nvar'-1'  {
       local thisvar : word `i' of `vlist'
       local nextvar: word  `=`i' + 1' of `vlist'
       replace `thisvar' = `nextvar' if BadObservation
    }
    //
    local numeric_vars = list of variables you want to be numeric
    destring `numeric_vars', replace

    Comment


    • #3
      Thank you, Mike. That worked perfectly. I apologize for not using dataex - I am working on a remote server which doesn't let me copy any of the data to an external environment.

      Comment


      • #4
        Hi,
        I have almost similar datasets as above. I want to shift the number (only the observation with number and not the letter) in the dform variable to the dose variable on the left. Same for number in the freq variable to the dose variable. The number should be added on to the existing number in the dose variable and not replacing the number. I have tried the command below but it did not work for me. It said dform not found. Any help is highly appreciated. Thanks

        Code:
        * Example generated by -dataex-. To install: ssc install    dataex
        clear
        input str41 drug str26 dose str22 dform str27 freq
        " gabapentin "  " 300mg "       " po tab/cap" " eod-mon"
        " t gabapentin" " 300mg "       " po tab/cap" " eod-mon"
        " gabapentin"   " 300mg"        " po tab/cap" " eod-mon"
        " t gabapentin" " 300mg (esrf)" " po tab/cap" " eod-mon"
        " gabapentin"   " 300mg"        " po tab/cap" " eod-mon"
        " gabapentin"   " 900(om)"      "900(pm)"     "1200(on)"
        " t gabapentin" " 300mg"        "300mg"       "600mg"   
        " gabapentin"   " 300mg"        "300mg"       "600mg"   
        " gabapentin"   " 300mg"        "300mg"       "600mg"   
        " t gabapentin" " 300mg"        "300mg"       "600mg"   
        " gabapentin"   " 300mg"        "300mg"       "600mg"   
        end

        forval i = 1/`=colsof(dform)' {
        local char : subinstr local char " " "", `i'
        if regexm("`char'", "[0-9]") {
        replace dform = dform + "`char'"
        }
        }

        Thanks for help.

        Comment

        Working...
        X