命令变量出现在proc freq的绘图输出中

时间:2019-04-16 21:57:21

标签: sas

我使用proc freq中的plot选项创建了频率图。但是,我无法订购所需的东西。我的类别为“ 5至10周”,“大于25周”,“ 10至15周”,“ 15至20周”。我希望他们按照增加周数的逻辑顺序去做,但是我不确定该怎么做。我尝试使用订单选项,但似乎没有任何解决办法。

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

尝试了order =选项,但这不能解决问题。

我希望垃圾箱显示为“少于5周”,“ 5至10周”,“ 10至15周”,“ 15至20周”,“ 20至25周”,“大于25周”

1 个答案:

答案 0 :(得分:0)

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;
相关问题