首页 > 解决方案 > 命令变量出现在 proc freq 的绘图输出中

问题描述

我使用 proc freq 中的 plot 选项创建了一个频率图。但是,我无法订购我想要的。我有“5 到 10 周”、“大于 25 周”、“10 到 15 周”、“15 到 20 周”的类别。我希望他们按照增加周数的逻辑顺序进行,但我不知道该怎么做。我尝试使用 order 选项,但似乎没有任何解决办法。

一个可能的解决方案是将我想要的顺序编码为 1-5 的值,使用 order= 选项对它们进行排序,然后为 1-5 设置一个标签。但我不确定这是否可能。

但是,尝试了 order= 选项,但这并没有解决问题。

我希望垃圾箱显示为“不到 5 周”“5 到 10 周”“10 到 15 周”“15 到 20 周”“20 到 25 周”“超过 25 周”

标签: sas

解决方案


Proc FREQ绘图按字母顺序显示表变量值并且order=未指定绘图选项时,您有以下情况

  • 变量是字符
  • 显示顺序为默认 ( INTERNAL)

注意:其他频率绘图技术,例如SGPLOT VBAR识别中点轴规范,可以控制字符值出现的显式顺序。 Proc FREQ没有 mxaxis 的绘图选项。

您正确地假设从标签到所需有序值的逆映射(或重新映射或取消映射)是必不可少的。这是重新映射的两种主要方法

  • 将标签映射到字符值的自定义格式(通过PUT
  • 将标签映射到数值的自定义信息(通过INPUT

将标签重新映射到值后,您需要第二种自定义格式将值映射回原始标签。

例子:

* format to map unmapped labels back to original labels;
proc format;
  value category
  1 = 'Less than 5 weeks'
  2 = '5 to 10 weeks'
  3 = '10 to 15 weeks'
  4 = '15 to 20 weeks'
  5 = '20 to 25 weeks'
  6 = 'Greater than 25 weeks'
  ;

  * informat to unmap labels to numeric with desired freq plot order;
  invalue category_to_num
  'Less than 5 weeks'     = 1
  '5 to 10 weeks'         = 2
  '10 to 15 weeks'        = 3
  '15 to 20 weeks'        = 4
  '20 to 25 weeks'        = 5
  'Greater than 25 weeks' = 6
  ;

* generate sample data;    
data have;
  do itemid = 1 to 500;
    cat_num = rantbl(123,0.05,0.35,0.25,0.15,0.07);  * for demonstration purposes;
    cat_char = put(cat_num, category.);              * your actual category values;
    output;
  end;
run;

* demonstration: numeric category (unformatted) goes ascending internal order;
proc freq data=have;
  table cat_num / plots=freqplot(scale=percent) ;
run;

* demonstration: numeric category (formatted) in desired order with desired category text;
proc freq data=have;
  table cat_num / plots=freqplot(scale=percent) ;
  format cat_num category.;
run;

* your original plot showing character values being ordered alphabetically
* (as is expected from default order=internal);
proc freq data=have;
  table cat_char / plots=freqplot(scale=percent) ;
run;

* unmap the category texts to numeric values that are ordered as desired;
data have_remap;
  set have;
  cat_numX = input(cat_char, category_to_num.);
run;

* table the numeric values computed during unmap, using format to display
* the desired category texts;
proc freq data=have_remap;
  table cat_numX / plots=freqplot(scale=percent) ;   * <-- cat_numX ;
  format cat_numX category.;                         * <-- format ;
run;

推荐阅读