Search code examples
saslabelproc

Is there a way in SAS to print the value of a variable in label using proc sql?


I have a situation where I would like to put the value of a variable in the label in SAS. Example: Median for Total_Days is 2. I would like to put this value in Days_Median_Split label. The median keeps on changing with varying data, so I would like to automate it.

  • Phy_Activity Total_Days "Days_Median_Split: Number of Days with Median 2"
  • No 0 0
  • No 0 0
  • Yes 2 1
  • Yes 3 1
  • Yes 5 1

Sample Dataset

Thanks so much!


Solution

  • * step 1 create data;
    data have;
    input Phy_Activity $ Total_Days Days_Median_Split;
    datalines;
    No 0 0
    No 0 0
    Yes 2 1
    Yes 3 1 
    Yes 5 1
    run;
    
    *step 2 sort data on Total_days;
    proc sort data = have;
    by Total_days;
    run;
    
    *step 3 get count of obs;
    proc sql noprint;
    select count(*) into: cnt
    from have;quit;
    
    * step 4 calulate median;
    %let median = %sysevalf(&cnt/2 + .5);
    
    
    *step 5 get median obsevation;
    proc sql noprint;
    select Total_days into: medianValue
    from have
    where monotonic()=&median;quit;
    
    *step 6 create label;
    data have;
    set have;
    label Days_Median_split = 'Days_Median_split: Number of Days with Median '  
    %trim(&medianValue);
    run;