Search code examples
sas

Character Range values in SAS proc format


I would like to group a dataset by variable SchoolSize,however group small and medium school sizes together, using proc format statement in SAS. My code below is not returning a means table grouped by small and medium school sizes together. It is separately grouping School sizes as L, M and S as specified in the original SAS file. Both original and alternate codes are not giving the grouping I need.

Original code:

proc format;
`value $SchoolSize
"L"="Large"
OTHER="Small and Medium";`
run;
title "Means of Data Set COLLEGE5";
proc means data=college5 mean median maxdec=2;
class  SchoolSize;
var ClassRank GPA;
format SchoolSize SchoolSize$.;
run;

Alternate code-1:

proc format;
`value $SchoolSize
"L"="Large"
"S"="Small and Medium"
"M"="Small and Medium";`
run;
title "Means of Data Set COLLEGE5";
proc means data=college5 mean median maxdec=2;
class  SchoolSize;
var ClassRank GPA;
format SchoolSize SchoolSize$.;

Alternate code-2:

proc format;
`value $SchoolSize
"L"="Large"
"S", "M"="Small and Medium";`
run;
title "Means of Data Set COLLEGE5";
proc means data=college5 mean median maxdec=2;
class  SchoolSize;
var ClassRank GPA;
format SchoolSize SchoolSize$.;`


Solution

  • On the FORMAT statement, the $ sign comes before the format name. So you want $schoolsize. rather than schoolsize$. I'm surprised the code as is doesn't generate an error message. Also, there are extra single quote marks in your PROC FORMAT steps. Other than that, I think all three approaches you showed should work. I tend to like the last approach. Below works.

    data class ;
      set sashelp.class ;
      if _N_<=4 then SchoolSize='S' ;
      else if _N_ <=8 then SchoolSize='M' ;
      else SchoolSize='L' ;
    run ;
    
    proc format;
      value $SchoolSize
        "L"="Large"
        "S","M"="Small and Medium"
      ;
    run;
    
    proc means data=class ;
      class schoolsize ;
      var weight ;
      format schoolsize $schoolsize. ;
    run ;