Proc Univariate

 

SAS Day 36

In SAS Day 27, we showed using Proc Means to generate the statistical summaries for Continuous Variable such as (Age, BMI, Height, Weight). As an old idiom stated, “All Roads lead to Rome”.  Today we will introduce Proc Univariate to create the Summary Statistics.

 

The_Double_A / Pixabay

 

Task: Proc Univariate Sample for Statistical Summaries

 

Sample Dataset :

n: patient ID number

trt: treatment group 

AGE: patient age

 

Output:

 

Sample Code:

%macro uni(in=, var=, output=, sorter=); 

proc univariate data=&in noprint; 
  var &var; 
  class trt; 
  output out=x n=nn mean=meann  std=stdn median=mediann min=minn max=maxn q1=q1n q3=q3n ; 
run; 

data x; 
  set x; 
    length n $3. mean $20. median $25. sd $8.3 q13$13. mm $13. ; 
    mean=put(meann,8.1); 
    median=put(mediann,3.) || "(" || put(minn,3.)||'-'||put(maxn,3.) ||")" ; 
    sd=put(stdn,8.2) ; 
    q13=put(q1n,6.1)||'-'||put(q3n,6.1); 
    mm=put(minn,6.1)||'-'||put(maxn,6.1); 
  n=put(nn,3.); 
  label n='   N' 
        mean='   Mean' 
        median='   Median'  
        sd="   Standard deviation" 
        q13="   25th-75th percentile" 
        mm='   Min-Max'; 
  keep n mean median mm q13 sd trt; 
run; 

  
proc transpose data=x out=&output(drop=_NAME_) prefix=col; 
  var n mean  median mm sd q13; 
  id trt; 
run; 


data &output; 
  length label $100. col1 $40. col2 $40.  ; 
  set &output; 
  label=_LABEL_; 
  if label='   N' then sorter2=1; 
  else if label='   Mean' then sorter2=2; 
  else if label='   Median(range)' then sorter2=3; 
  else if label="   Standard deviation" then sorter2=4; 
  else if label="   25th-75th percentile" then sorter2=5; 
  else if label='   Min-Max' then sorter2=6; 
  sorter= &sorter; 
  keep sorter sorter2 label col:; 
proc sort; by sorter2; 
run; 
%mend uni; 

%uni(in=adsl, var=age, sorter= 1, output=a1);

 

Note: Special thanks to Alex Tian (Lipu), the sample code was originated by him. I modified the label and order along the time.
so maybe you could do your Mix and Match with this sample Proc Univariate code.

Happy Practicing !💤

Post navigation

  One thought on “Proc Univariate

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: