Survival Analysis in Stata
Analyze a cancer drug trial dataset using Kaplan-Meier curves and Cox regression.
Introduction
30 Second Summary
When a clinical trial tests whether a new cancer drug helps patients survive longer, comparing simple averages won't work. Some patients are still alive when the study ends, creating incomplete data called censoring that requires specialized statistical methods.
In this project, you will analyze a cancer drug trial dataset in Stata using survival analysis. You will visualize who survives longer, test whether the difference is statistically real, and build a regression model that accounts for patient age.
What You'll Build
Picture running a single command and seeing a hazard ratio that tells you exactly how much a drug cuts a cancer patient's risk of death, confirmed by diagnostic tests you ran yourself.
By the end of this project, you'll have:
- Kaplan-Meier survival curves showing at a glance that drug patients survive longer than placebo patients.
- A Cox proportional hazards model with interpreted hazard ratios telling you exactly how much the drug reduces the risk of death after controlling for patient age.
- Diagnostic test output proving your model's proportional hazards assumption holds, so you can trust every number in your results.
- An optional Secret Mission to push your survival analysis skills even further.
Are there any prerequisites?
You need Stata 19 accessible through your university or institution. No prior Stata or statistics experience is required.
Before We Start
Before we dive into any commands, let's lock in what you're about to build and why it matters.
Get Oriented in Stata and Load the Data
You know what you are analyzing and why survival analysis is the right tool. Now you need to actually see the data.
Before you can run any statistical commands, you need to open Stata, learn where everything lives in the interface, and load the cancer drug trial dataset. By the end of this step, you will know exactly what variables you are working with and how many patients are in each treatment group.
In this step, get ready to:
- Identify Stata's five main windows and confirm that commands work.
- Load the built-in cancer drug trial dataset and explore its structure.
- Browse the raw data to understand each variable.
Open Stata and find your way around the interface
Stata's interface is split into five windows that stay open the entire time you work. Finding them now saves confusion later.
- Press the Windows key to open your search bar.
- Type Stata and press Enter to open it.
Once Stata opens, look at the main window. You should see five named panels arranged inside it.
- Find the Command window at the bottom. This is where you type commands.
- Find the Results window in the center. This is where Stata prints output.
- Find the History window on the left. This logs every command you run.
- Find the Variables and Properties windows on the right. These show information about the loaded dataset.
What are these five windows for?
You type a command in the Command window, Stata runs it, and the output appears in the Results window. The History window keeps a running list of everything you have typed so you can click to re-run a previous command.
The Variables window lists every column in your dataset once one is loaded. The Properties window shows metadata about a selected variable or the dataset itself.
- Confirm commands work by clicking inside the Command window, typing pwd, and pressing Enter.
Stata prints your current working directory in the Results window. If you see a file path, your setup is working.
- Open the Do-file Editor by clicking the Do-file Editor button in the toolbar.
What is the Do-file Editor?
A do-file is a script that contains a list of Stata commands you can run all at once. The Do-file Editor is Stata's built-in text editor for writing and running these scripts. Instead of retyping every command, you save them in a .do file and re-run the whole analysis with one click.
A new, blank tab opens in the Do-file Editor. This is where you will build up your analysis script.
- Save this blank file by pressing Ctrl+S.
- In the save dialog, name the file survival_analysis.do and save it to a location you can find easily, like your Desktop.
Don't see the Do-file Editor button?
Hover over the toolbar buttons until you see a tooltip that says Do-file Editor. You can also type doedit in the Command window and press Enter to open it directly.
Help me find the Do-file Editor in Stata.
Load the cancer drug trial dataset and explore it
Stata ships with several practice datasets built right in. The one you need is called cancer, and it contains artificial data on 48 patients in a drug trial.
- Type the following into the Do-file Editor, starting at the top of the file:
* ============================================
* Survival Analysis of Cancer Drug Trial Data
* ============================================
* --- Step 1: Load and Explore the Data ---
sysuse cancer, clear
describe
- Select all the lines you just typed.
- Press Ctrl+D to execute the selected lines.
What does this code do?
Lines starting with * are comments. Stata ignores them, but they help you organize your script.
sysuse cancer, clear loads the built-in cancer dataset into memory. The clear option tells Stata to replace any data already in memory.
describe lists every variable in the dataset along with its type and label.
In the Results window, you should see a table listing four variables: studytime, died, drug, and age. The Variables window on the right also populates with these four names.
- Now add these two exploration commands below the existing code in the Do-file Editor:
summarize
tabulate drug
- Select only the two new lines you just added.
- Press Ctrl+D to execute them.
What does this output tell you?
summarize prints summary statistics for every variable: count, mean, standard deviation, min, and max. You should see 48 observations across all variables.
tabulate drug shows a frequency table for the drug variable. You will see three groups: drug 1 (placebo), drug 2, and drug 3 (two different treatments). This tells you how many patients are in each treatment arm.
Check the Results window. The summarize output confirms 48 observations, and tabulate drug shows three distinct drug groups.
Seeing an error after pressing Ctrl+D?
Make sure you selected only the new lines before pressing Ctrl+D. If you selected the entire file including sysuse cancer, clear, it will re-run the load command (which is fine). If you see a syntax error, check for typos in your commands.
Help me fix a Stata command error.
Browse the raw data to understand each variable
Summary statistics give you the big picture, but you should also look at the actual rows to understand what each variable means for a single patient.
- Add this command below the existing code in the Do-file Editor:
list in 1/10
- Select the new line and press Ctrl+D to execute it.
What does this output show?
list in 1/10 displays the first 10 rows of data in the Results window. Each row is one patient. Here is what the columns mean:
- studytime: months until the patient died or the observation period ended.
- died: 1 if the patient died during the study, 0 if they were still alive when observation stopped (this is called censoring).
- drug: 1 = placebo, 2 and 3 = two different treatment drugs.
- age: the patient's age at the start of the experiment.
Look at a patient with died = 0 in the output. That patient did not necessarily survive. Their observation was simply cut short. This is exactly why you cannot just compare average survival times between groups.
- Save your do-file by pressing Ctrl+S in the Do-file Editor.
What is censoring?
A censored observation is a patient whose final outcome you never observed. Maybe the study ended before they died, or they dropped out. You know they survived at least that long, but not how much longer. Survival analysis methods like Kaplan-Meier curves are specifically designed to handle this incomplete information, which is why they are more accurate than simple averages.
Before you move on, take one more look at the full do-file to make sure everything is in order.
✔️ Awesome, I've got everything!
Great. Double check you have saved survival_analysis.do with Ctrl+S.
ⓧ I'd like to double check the full code
Your survival_analysis.do file should look exactly like this:
* ============================================
* Survival Analysis of Cancer Drug Trial Data
* ============================================
* --- Step 1: Load and Explore the Data ---
sysuse cancer, clear
describe
summarize
tabulate drug
list in 1/10
What do you expect to see when you run summarize on this dataset? Think about the number of observations and the range of ages before checking.
Run the entire file by selecting all lines in the Do-file Editor and pressing Ctrl+D. In the Results window, you should see 48 observations with variables studytime, died, drug, and age. The age range runs from 47 to 67, and there are three drug groups.
Output looks different or shows errors?
If you see fewer than 48 observations, you may have loaded a different dataset. Confirm the line reads sysuse cancer, clear exactly. If Stata reports a command not found, check for extra spaces or missing letters in your commands.
Help me troubleshoot my Stata do-file.
You have your data loaded and you know what every column means. Next up, you will teach Stata that this is survival data and see your first visual payoff: Kaplan-Meier curves showing whether the drug patients actually survive longer.
Declare Survival Data and Visualize Survival Curves
You have the cancer dataset loaded and you know what each variable represents. You have 48 patients across 3 treatment groups, with studytime tracking months until death or end of observation.
Right now, Stata treats studytime and died as ordinary numbers. Its survival analysis commands will not work until you formally declare which variable is the time-to-event and which marks the event. Once you do that, you can see your first visual payoff: Kaplan-Meier survival curves showing whether drug patients actually survive longer.
In this step, get ready to:
- Declare the dataset as survival data so Stata's survival commands recognize the time and event variables.
- Plot Kaplan-Meier survival curves to visualize survival differences between drug groups.
Declare the dataset as survival data
Stata's survival analysis tools (sts, stcox, streg) require you to declare your data's structure first. The stset command tells Stata which variable represents time-to-event and which variable indicates whether the event occurred.
- In the Do-file Editor, add the following lines below your Step 1 code:
* --- Step 2: Declare Survival Data and Visualize ---
stset studytime, failure(died)
- Highlight the stset studytime, failure(died) line.
- Click the Do button in the Do-file Editor toolbar to run the selected line.
What does this command do?
stset is Stata's command for declaring survival data. It configures two things:
- studytime is the time variable (months until death or end of observation).
- failure(died) tells Stata that died is the event indicator. A value of 1 means the patient died. A value of 0 means the patient was still alive when observation ended. This is called censoring.
In the Results window, Stata reports the survival data setup. You should see 48 total observations, 31 failures, and 744 total analysis time at risk.
- Now add stdescribe below the stset line in the Do-file Editor:
stdescribe
- Highlight the stdescribe line and click the Do button to run it.
The Results window now shows a detailed breakdown of the survival data structure, including the number of subjects, records per subject, and time at risk. This confirms the data is properly configured for survival analysis.
Seeing an error after stset?
- Check that you typed the variable names exactly as studytime and died. Stata variable names are case-sensitive.
- Make sure the cancer dataset is still loaded. If you closed and reopened Stata, run sysuse cancer, clear first to reload it.
Still stuck? Help me troubleshoot my stset command.
Plot Kaplan-Meier survival curves
Now that Stata knows the data structure, you can visualize survival probabilities over time. A Kaplan-Meier curve plots the estimated probability of surviving beyond each time point.
- Add the following line below stdescribe in the Do-file Editor:
sts graph
- Highlight the sts graph line and click the Do button to run it.
A Graph window opens showing the overall Kaplan-Meier survivor function for all 48 patients combined. The curve starts at 1.0 (everyone alive) and steps down each time a patient dies.
What does a Kaplan-Meier curve show?
Each step down in the curve represents one or more patients dying at that time point. The height of the curve at any time tells you the estimated probability that a patient survives beyond that point.
A curve that drops steeply means patients are dying quickly. A curve that stays high means patients are surviving longer. This single curve combines all three drug groups, so it tells you the overall story but not whether the drug helps.
The overall curve is interesting, but the real question is whether the drug groups survive differently. To answer that, you need separate curves for each group.
- Add the following line below sts graph in the Do-file Editor:
sts graph, by(drug)
Before you run this, what do you expect to see? Will the drug groups have higher or lower survival curves than the placebo group?
- Highlight the sts graph, by(drug) line and click the Do button to run it.
A new Graph window opens with three distinct survival curves, one for each drug group. The placebo group (drug=1) drops much faster than the two treatment groups. The treatment groups stay higher, meaning those patients survived longer.
What do the three curves tell you?
The visual separation between the curves suggests the drug is working. Patients on the placebo die sooner, while patients on the treatments survive longer.
But is the difference statistically real, or could it be due to chance? Your eyes can see a gap, but you need a formal test to confirm it. That is exactly what you will build in the next step.
Graph window not appearing?
- The Graph window may open behind the main Stata window. Check your taskbar for a Stata Graph window and click it to bring it forward.
- Make sure you ran stset studytime, failure(died) before running sts graph. The sts commands require survival data to be declared first.
Still stuck? Help me troubleshoot my Kaplan-Meier graph.
✔️ Awesome, I've got everything!
Great. Double check you have saved your do-file.
ⓧ I'd like to double check the full code
Here is the full survival_analysis.do file so far. Make sure your do-file matches this exactly:
* ============================================
* Survival Analysis of Cancer Drug Trial Data
* ============================================
* --- Step 1: Load and Explore the Data ---
sysuse cancer, clear
describe
summarize
tabulate drug
list in 1/10
* --- Step 2: Declare Survival Data and Visualize ---
stset studytime, failure(died)
stdescribe
sts graph
sts graph, by(drug)
Your survival curves clearly show the drug groups surviving longer than the placebo group. Next up, you will run a formal statistical test to confirm this difference is real, and discover a hidden problem that the curves alone cannot reveal.
Test the Difference and Discover a Problem
Your Kaplan-Meier curves from the last step tell a compelling story. The placebo group's survival curve drops noticeably faster than either treatment group.
Visual differences can be deceiving, though. You need a formal statistical test to know whether the gap between those curves is real or just random variation.
In this step, get ready to:
- Run the log-rank test to determine if survival differs significantly across drug groups.
- Discover a confounding variable that the log-rank test cannot account for.
Run the log-rank test
The log-rank test compares survival curves across groups and produces a chi-squared statistic with a p-value. A p-value below 0.05 means the differences are statistically significant.
Before you run this test, think about what you expect. Based on those Kaplan-Meier curves you just plotted, do you think the survival difference will be statistically significant?
- Add the following code to your Do-file Editor below the Step 2 commands, then select the new lines and click the Execute (do) button to run them:
* --- Step 3: Test the Difference and Discover a Problem ---
sts test drug
What does this output show?
Stata displays a table with observed and expected events for each drug group, followed by a chi-squared statistic and a p-value.
If the p-value (shown as Pr>chi2) is below 0.05, you can conclude that the survival curves are significantly different across the three drug groups.
Look at your Results window. You should see a log-rank test output with a chi-squared value and a p-value confirming the survival difference is statistically significant.
Not seeing test output?
- Make sure you selected both new lines (the comment and the sts test drug command) before clicking the Execute (do) button.
- Confirm your data is still declared as survival data. If you restarted Stata, you need to re-run sysuse cancer, clear and stset studytime, failure(died) before this command will work.
- help me troubleshoot why my log-rank test is not producing output in Stata
Investigate a potential problem
The log-rank test says the drug groups have significantly different survival. Case closed? Not quite.
Before you dig deeper, think about this: could anything other than the drug itself explain why some groups survived longer? What if the treatment groups happened to have younger patients?
- Add this command to your Do-file Editor below sts test drug, then select it and click the Execute (do) button to run it:
tabulate drug, summarize(age)
Look at the average ages across the three drug groups in the Results window. The groups have noticeably different age distributions.
What is confounding?
A confounding variable is something that affects both the treatment assignment and the outcome. Here, if older patients were more likely to end up in the placebo group, age is confounding the relationship between drug and survival.
The log-rank test has no way to control for confounders. It compares survival curves directly without adjusting for other variables like age.
This is the blind spot. Your log-rank test gave a significant result, but it cannot separate the drug's true effect from the effect of age differences between groups.
✔️ Awesome, I've got everything!
Great. Make sure you have saved your survival_analysis.do file in the Do-file Editor.
ⓧ I'd like to double check the full code
Here is the full survival_analysis.do file up to this point. Compare it with what you have in your Do-file Editor.
* ============================================
* Survival Analysis of Cancer Drug Trial Data
* ============================================
* --- Step 1: Load and Explore the Data ---
sysuse cancer, clear
describe
summarize
tabulate drug
list in 1/10
* --- Step 2: Declare Survival Data and Visualize ---
stset studytime, failure(died)
stdescribe
sts graph
sts graph, by(drug)
* --- Step 3: Test the Difference and Discover a Problem ---
sts test drug
tabulate drug, summarize(age)
You have confirmed the survival difference is statistically significant, but discovered it could be confounded by age. Next, you will build a Cox proportional hazards model that separates the drug's effect from the effect of age.
Build a Cox Proportional Hazards Model
Your log-rank test confirmed that survival differs significantly across drug groups. But you also discovered a problem: the groups have different average ages, and older patients die sooner regardless of treatment.
The Cox proportional hazards model solves this by estimating how much each factor independently contributes to the risk of death. It gives you hazard ratios that separate the drug's effect from the effect of age. In this step, you will fit a Cox model and interpret what it tells you.
In this step, get ready to:
- Fit a Cox proportional hazards model with drug and age as covariates.
- Interpret hazard ratios and raw coefficients to understand each variable's independent effect on survival.
Fit the Cox model and interpret hazard ratios
The log-rank test treats survival as an all-or-nothing comparison between groups. The Cox model goes further by estimating a separate coefficient for each variable, so you can measure the drug's effect while holding age constant.
Before you run this model, think about what you expect. Based on the Kaplan-Meier curves from Step 2, do you think the drug's hazard ratio will be above 1 or below 1?
- In the Do-file Editor, add the following code at the end of your survival_analysis.do file:
* --- Step 4: Build Cox Proportional Hazards Model ---
stcox drug age
- Select the new stcox drug age line and run it by clicking the Execute (do) button in the Do-file Editor toolbar.
The Results window displays a regression table. At the top, you see the number of subjects, failures, and time at risk. Below that is a table with columns for Haz. ratio, Std. err., z, P>|z|, and a 95% confidence interval.
What do these hazard ratios mean?
A hazard ratio is a multiplier on the risk of death. It tells you how much a one-unit change in a variable increases or decreases that risk, after controlling for the other variables in the model.
- drug hazard ratio is below 1. This means each step up in drug group (from placebo toward treatment) reduces the risk of death. The drug works.
- age hazard ratio is above 1. This means each additional year of age increases the risk of death, even after accounting for treatment group.
Seeing "last estimates not found" or no output?
- Make sure you ran stset studytime, failure(died) in Step 2 first. The stcox command requires data that has been declared as survival data.
- Check that you typed stcox drug age with spaces between each word. No commas between the variable names.
- If you restarted Stata since Step 2, you need to re-run all previous commands first. Select all lines from the top of your do-file through stcox drug age and execute them together.
Still stuck? Help me troubleshoot my stcox command.
Examine the model summary and view raw coefficients
The regression table also includes a model-level summary above the coefficient rows. This tells you whether the model as a whole is statistically meaningful.
- Scroll up in the Results window to find the top of the stcox output.
- Find No. of subjects (should be 48), No. of failures (the number of deaths), and Time at risk (total months observed across all patients).
- Find the LR chi2 line and its Prob > chi2 value. If this p-value is below 0.05, the overall model is statistically significant.
What does the likelihood ratio test tell you?
The likelihood ratio chi-squared test asks: does this model (with drug and age) fit the data better than a model with no covariates at all? A small p-value means yes, your variables genuinely help predict survival.
Hazard ratios are easy to interpret, but sometimes you want to see the raw coefficients that sit behind them. These show the direction and magnitude of each effect on the log-hazard scale.
- Add the following line to the end of your do-file, below stcox drug age:
stcox drug age, nohr
- Select the new line and run it by clicking Execute (do).
The Results window now shows the same model, but the Haz. ratio column is replaced by a Coef. column showing raw log-hazard coefficients.
Why view raw coefficients?
A negative coefficient means the variable reduces the hazard (risk of death). A positive coefficient means it increases the hazard. These are the log-transformed versions of the hazard ratios you saw earlier.
The nohr option is useful when you want to see the direction of the effect at a glance. Negative for drug confirms it is protective. Positive for age confirms it is a risk factor.
Output looks identical to the first run?
- Check that you included the comma before nohr. The correct syntax is stcox drug age, nohr. Without the comma, Stata treats nohr as a variable name instead of an option.
- Look at the column header. If it says Coef. instead of Haz. ratio, the command worked correctly.
Need help reading the output? Help me understand the stcox nohr output.
✔️ Awesome, I've got everything!
Great. Make sure you have saved survival_analysis.do with both stcox commands before moving on.
ⓧ I'd like to double check the full code
Here is the complete survival_analysis.do file through the end of Step 4. Compare it against your do-file line by line.
* ============================================
* Survival Analysis of Cancer Drug Trial Data
* ============================================
* --- Step 1: Load and Explore the Data ---
sysuse cancer, clear
describe
summarize
tabulate drug
list in 1/10
* --- Step 2: Declare Survival Data and Visualize ---
stset studytime, failure(died)
stdescribe
sts graph
sts graph, by(drug)
* --- Step 3: Test the Difference and Discover a Problem ---
sts test drug
tabulate drug, summarize(age)
* --- Step 4: Build Cox Proportional Hazards Model ---
stcox drug age
stcox drug age, nohr
Your Cox model shows the drug significantly reduces the risk of death, even after controlling for age. But can you trust these numbers? The Cox model rests on a critical assumption: that hazard ratios stay constant over time. In the next step, you will test whether that assumption actually holds.
Verify the Proportional Hazards Assumption
Your Cox model showed that the drug significantly reduces mortality risk after controlling for age. Those hazard ratios told a compelling story.
But the Cox model assumes that hazard ratios stay constant over time. This is the proportional hazards assumption. If it is violated, your results from Step 4 cannot be trusted.
In this step, get ready to:
- Test the proportional hazards assumption with the Schoenfeld residuals test.
- Create log-log plots to visually confirm the assumption holds.
Run the Schoenfeld residuals test
The proportional hazards assumption means that the effect of each variable on survival does not change over time. If the drug cuts the hazard in half at month 5, it should still cut it in half at month 15.
The Schoenfeld residuals test checks this formally for each covariate and for the overall model. Before you run the test, what do you expect? Do you think the proportional hazards assumption will hold?
- Type the following command in the Command window and press Enter:
estat phtest, detail
What do these results mean?
The output shows a row for each covariate (drug and age) plus a global test at the bottom.
Each row has a chi-squared value and a p-value. A p-value above 0.05 means you cannot reject the proportional hazards assumption for that variable. The global test at the bottom tells you whether the overall model satisfies the assumption.
You want p-values above 0.05 here. That is the outcome that supports your Cox model.
Look at the global test p-value in the Results window. If it is above 0.05, your Cox model's proportional hazards assumption holds.
Seeing an error?
- estat phtest runs on the most recent stcox results in memory. If you ran other commands or restarted Stata, the model may no longer be in memory.
- Re-run stcox drug age first, then try estat phtest, detail again.
Why is my estat phtest command failing?
Create a graphical diagnostic
A formal test gives you a number, but a visual check adds confidence. The stphplot command creates log-log survival plots that show whether hazard ratios are truly constant over time.
- Create the graphical diagnostic by typing the following command in the Command window and pressing Enter:
stphplot, by(drug)
What does this graph show?
The plot shows -ln[-ln(S(t))] versus ln(t) for each drug group. If the proportional hazards assumption holds, these curves should be roughly parallel.
Minor wiggles are normal, especially with a small dataset of 48 patients. What you do not want to see is the lines crossing or dramatically diverging over time.
The curves should be roughly parallel. This visual confirmation, combined with the formal test, proves your Cox model's assumptions hold and its results are trustworthy.
Not seeing a graph?
- Make sure survival data is still declared. If you restarted Stata, you need to reload the data with sysuse cancer, clear and re-run stset studytime, failure(died) before trying the plot again.
- Check that your Graph window is open. In the Stata menu bar, click Window and look for the graph window in the list.
Why is stphplot not producing a graph in Stata?
- Switch back to the Do-file Editor and find the last line (stcox drug age, nohr).
- Add the Step 5 commands below it by typing the following:
* --- Step 5: Verify Proportional Hazards Assumption ---
estat phtest, detail
stphplot, by(drug)
What does this section do?
- estat phtest, detail runs the Schoenfeld residuals test on the most recent Cox model, checking whether each covariate satisfies the proportional hazards assumption.
- stphplot, by(drug) creates a log-log plot for each drug group so you can visually confirm the curves are roughly parallel.
- Save your do-file by pressing Ctrl+S.
✔️ Awesome, I've got everything!
Double check you have saved your do-file. Your survival_analysis.do should now contain all five steps of the analysis.
ⓧ I'd like to double check the full code
Here is the complete do-file with all five steps. Compare it against yours line by line.
* ============================================
* Survival Analysis of Cancer Drug Trial Data
* ============================================
* --- Step 1: Load and Explore the Data ---
sysuse cancer, clear
describe
summarize
tabulate drug
list in 1/10
* --- Step 2: Declare Survival Data and Visualize ---
stset studytime, failure(died)
stdescribe
sts graph
sts graph, by(drug)
* --- Step 3: Test the Difference and Discover a Problem ---
sts test drug
tabulate drug, summarize(age)
* --- Step 4: Build Cox Proportional Hazards Model ---
stcox drug age
stcox drug age, nohr
* --- Step 5: Verify Proportional Hazards Assumption ---
estat phtest, detail
stphplot, by(drug)
Secret mission
Fit a Parametric Weibull Survival Model
Your Cox model proved the drug works, but it left something unanswered: is the risk of death changing over time? A parametric Weibull model can answer that question. Fit one, interpret its shape parameter, and compare the results to your Cox model.
Clean Up Your Resources
Clean Up Your Resources
This project runs entirely on your local machine using Stata's built-in dataset, so there are no ongoing costs or cloud resources to worry about. Decide whether to keep your do-files for future reference or delete them.
Resources you used:
- survival_analysis.do (main analysis do-file with Steps 1 through 5).
- survival_analysis_weibull.do (Secret Mission Weibull model do-file).
Keep everything running
No action needed. Choose this if you plan to revisit the analysis or extend it with new models.
- Your do-files stay wherever you saved them on your machine.
- The cancer dataset is built into Stata and loads fresh every time you run sysuse cancer, clear.
- Stata is licensed through your university. No changes are needed to your Stata installation.
Pause - I'll come back to this later
Close Stata to free up memory. Your do-files remain on disk for when you return.
- Save any open do-files in the Do-file Editor before closing Stata.
- Close Stata. When you return, reopen your do-files and run them from the top to restore your analysis state.
Delete - I don't want to use this again
Remove your do-files and close Stata. The built-in dataset stays with Stata and does not need to be deleted.
- Close Stata.
- Open File Explorer and navigate to the folder where you saved your do-files.
- Select survival_analysis.do and survival_analysis_weibull.do, then press Delete.
- Empty your Recycle Bin to permanently remove the files.
Not sure where your files are?
Before closing Stata, type pwd in the Command window and press Enter. This shows Stata's current working directory, which is where your files were saved by default.
Nice Work!
Nice Work!
Nice work! You just completed a full survival analysis of a cancer drug trial in Stata. You went from raw clinical data to a validated statistical model that proves the drug significantly reduces the risk of death after controlling for patient age.
You've learned how to:
- Visualized survival differences with Kaplan-Meier curves and formally tested them with the log-rank test.
- Built a Cox proportional hazards model with hazard ratios that isolate the drug's effect on mortality after controlling for patient age.
- Validated the model by testing the proportional hazards assumption with Schoenfeld residuals and graphical diagnostics.
- Tackled an optional Secret Mission to push your survival analysis skills further.
Ready to quiz yourself?