PROC TRANSPOSE

The code calculates and displays the average systolic blood pressure for each combination of sex and smoking status in a transposed format, accounting for missing values in the smoking status variable.


Summary

The code performs the following tasks:

  1. Create a new dataset (heart_data) using the sashelp.heart dataset. In this step, it also creates a new variable called 'Smoking_Status_Group'. If the 'Smoking_Status' value is missing, the 'Unknown' category is assigned to the 'Smoking_Status_Group' variable. Otherwise, the original value is kept.
  2. Use PROC SUMMARY to calculate the average systolic blood pressure ('Systolic') by 'Sex' and 'Smoking_Status_Group'. The output dataset, avg_sbp, contains the mean systolic blood pressure for each combination of 'Sex' and 'Smoking_Status_Group'.
  3. Sort the avg_sbp dataset by 'Sex' and 'Smoking_Status_Group' to prepare it for the transposition step.
  4. Use PROC TRANSPOSE to transpose the avg_sbp dataset, pivoting on the 'Smoking_Status_Group' variable. This creates a new dataset called transposed_sbp, where each unique value of 'Smoking_Status_Group' becomes a new variable, and the corresponding 'Avg_Systolic' values fill in the dataset.
  5. Print the transposed_sbp dataset using PROC PRINT to display the results. The output shows the average systolic blood pressure for each combination of 'Sex' and 'Smoking_Status_Group' in a transposed format.

This code allows you to analyze the average systolic blood pressure for different groups based on sex and smoking status, with the results presented in a more compact and readable form.

* Use the sashelp.heart dataset;
data heart_data;
    length Smoking_Status_Group $200;
    set sashelp.heart;
    if missing(Smoking_Status) then
        Smoking_Status_Group = 'Unknown';
    else
        Smoking_Status_Group = Smoking_Status;
    keep Sex Systolic Smoking_Status_Group;
run;

* Calculate the average systolic blood pressure by Sex and Smoking_Status_Group;
proc summary data=heart_data nway;
    class Sex Smoking_Status_Group;
    var Systolic;
    output out=avg_sbp mean(Systolic)=Avg_Systolic;
run;

* Sort the dataset;
proc sort data=avg_sbp;
    by Sex Smoking_Status_Group;
run;

* Transpose the dataset;
proc transpose data=avg_sbp out=transposed_sbp;
    by Sex;
    id Smoking_Status_Group;
    var Avg_Systolic;
run;

* Print the transposed dataset;
proc print data=transposed_sbp;
run;
Share this Post