Announcement

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

  • Question regarding loop syntax

    Hi, I'm new to Stata and still figuring out loops.

    I'm creating a loop to clean two datasets. First I'm trying to rename some variables, then recode bmi into two categories. I am then trying to extract a substring from patient id to create an id with just numbers. I'm then saving the dataset and doing some checking.

    My issue is that when I check the cleaned dataset, while the recoded bmi. has been changed in the dataset and the variable renaming has changed, the new variable (pt_id) has been added to the dataset but it has no values.

    Can anyone suggest what I'm doing wrong?

    Thanks!

    * Define datasets BP1 and BP2
    local datasets "BP1.dta BP2.dta"

    * Loop through each dataset
    foreach dataset in `datasets' {

    * Load the dataset
    use `dataset', clear

    * Rename variables
    rename age_group agegp
    rename bmi_group bmigp
    rename employment_group empgp
    rename ses_group sesgp


    * Recode bmi level into two groups to match the dataset description
    replace bmigp = "0" if bmigp == "underweight" | bmigp == "normal"
    replace bmigp = "1" if bmigp == "overweight" | bmigp == "obese"

    * Extract numeric part from patient_id
    gen pt_id = substr(patient_id, 2, .)
    * Verify the extraction worked
    list id pt_id in 1/10

    * Save the cleaned dataset
    local cleaned_filename = "`dataset'_cleaned"
    save `cleaned_filename', replace
    display "Saved cleaned dataset: `cleaned_filename'.dta"
    *Summarize and check dataset
    summarize


    * Run codebook on bmigp
    display "Details for variable bmigp in `dataset'"
    codebook bmigp
    }


  • #2
    Code:
    gen pt_id = substr(patient_id, 2, .)
    will return an empty string if patient_id is just one character long.

    Otherwise here are some suggestions on your code. I created a numeric (0, 1) indicator. which is much more useful than a ("0", "1") indicator.

    Code:
    * Loop through each dataset
    foreach dataset in BP1 BP2 {
    
    * Load the dataset
    use `dataset', clear
    
    * Rename variables
    rename (age_group employment_group ses_group) (agegp empgp sesgp) 
    
    * Recode bmi level into two groups to match the dataset description
    gen bmigp == 0 if inlist(bmi_group, "underweight", "normal")
    replace bmigp = 1 if inlist(bmi_group, "overweight", "obese"), 
    
    * Extract numeric part from patient_id
    gen pt_id = substr(patient_id, 2, .)
    * Verify the extraction worked
    list id pt_id in 1/10
    
    * Save the cleaned dataset
    save "`dataset'_cleaned", replace
    display "Saved cleaned dataset: `cleaned_filename'.dta"
    
    *Summarize and check dataset
    summarize
    
    * Run codebook on bmigp
    display "Details for variable bmigp in `dataset'"
    codebook bmigp
    }

    Comment


    • #3
      I should have flagged here my interpretation: although the question is posed as being about loop syntax, that doesn't seem to be the issue, which you did flag concisely:

      the new variable (pt_id) has been added to the dataset but it has no values
      I take that to mean empty strings. Even your identifiers may be sensitive or confidential, and if so you shouldn't post them here, but I think this boils down to looking more carefully at your identifiers. Why do you want to remove the first character anyway?

      Fixed some small errors below.

      Code:
      * Loop through each dataset
      foreach dataset in BP1 BP2 {
      
      * Load the dataset
      use `dataset', clear
      
      * Rename variables
      rename (age_group employment_group ses_group) (agegp empgp sesgp)
      
      * Recode bmi level into two groups to match the dataset description
      gen bmigp == 0 if inlist(bmi_group, "underweight", "normal")
      replace bmigp = 1 if inlist(bmi_group, "overweight", "obese")
      
      * Extract numeric part from patient_id
      gen pt_id = substr(patient_id, 2, .)
      * Verify the extraction worked
      list id pt_id in 1/10
      
      * Save the cleaned dataset
      save "`dataset'_cleaned", replace
      display "Saved cleaned dataset: `dataset'_cleaned.dta"
      
      *Summarize and check dataset
      summarize
      
      * Run codebook on bmigp
      display "Details for variable bmigp in `dataset'"
      codebook bmigp
      
      }
      Last edited by Nick Cox; 26 May 2024, 05:33.

      Comment

      Working...
      X