如何在SAS中将医疗CSV数据集的三分类Readmitted变量转为二分类?
Recoding Readmitted to Binary for Logistic Regression in SAS
No problem at all—here are two clean, reliable ways to recode your Readmitted categorical variable into a binary (0/1) format for logistic regression in SAS:
Method 1: Create a New Binary Variable (DATA Step)
This approach makes a permanent new variable in your dataset, which is great if you'll use it across multiple analyses. Just adjust the dataset name (your_dataset) to match your actual data:
data cleaned_data; set your_dataset; /* Create binary variable: 0 = No readmission, 1 = any readmission */ if Readmitted = "No" then Readmit_bin = 0; else if Readmitted in (">30", "<30") then Readmit_bin = 1; /* Optional: Handle missing values if needed */ else Readmit_bin = .; /* Assign missing for any unexpected values */ run;
Method 2: Use a Custom Format (No New Variable)
If you don't want to add a new variable, you can define a format that maps the original values to 0/1 on the fly. This works well for one-off analyses or preserving the original variable:
/* Define the custom format */ proc format; value readmit_bin_fmt "No" = 0 ">30", "<30" = 1 other = .; /* Map invalid values to missing */ run; /* Apply the format in your logistic regression (or any procedure) */ proc logistic data=your_dataset; class Readmitted / param=ref; /* Optional, but useful for reference coding */ model Readmitted = your_predictors / link=logit; format Readmitted readmit_bin_fmt.; /* Apply the binary format */ run;
Quick Notes:
- Make sure the quotes around
Readmittedvalues match the actual data type—ifReadmittedis numeric instead of character, remove the quotes (e.g.,if Readmitted = 0 then ...assuming "No" was coded as 0 originally). - The
other = .clause is optional but recommended to catch any unexpected values (like typos) and flag them as missing, which keeps your analysis clean.
内容的提问来源于stack exchange,提问作者Justin Arnold
相关产品推荐
相关产品推荐

