【问题标题】:counting observations in sas在 sas 中计算观察值
【发布时间】:2018-06-15 10:23:54
【问题描述】:

我有过去 11 年(2006-2017 年)的 TB 病例数据,其中包含以下变量:年龄、病例状态、城市、年份、月份和种族。我想制作一个逐年的折线图,其中案例为 y 轴,月份为 x 轴,然后每年都有一条新线。但我不确定如何计算观察结果。我是 sas 的新手,所以如果我做错了,我提前道歉,但这就是我的想法:

    PROC SORT DATA= TB
    BY YEAR;
    RUN;

    DATA YEAR;
    SET TB;
    COUNT + 1;
    BY YEAR;
    IF FIRST.YEAR THEN COUNT =1;
    RUN;


    PROC SGPLOT DATA = YEAR;
    SERIES X = Month Y = COUNT;
    SERIES X = Year Y = COUNT;
    TITLE 'Temporal Patterns of TB from 2006-2017';
    RUN; 

但是我得到的这段代码的输出是空白的。

任何反馈/帮助将不胜感激! 提前谢谢!

【问题讨论】:

    标签: count sas


    【解决方案1】:

    如果您提供示例数据,这会容易得多。假设您正在尝试做类似于绘制折线图的事情,我将使用 SASHELP.PRDSALE 数据集作为演示。我在这里使用重量表来计算数量,但您不需要。

    首先运行 PROC FREQ 以获取汇总统计信息,然后使用 SGPLOT 中的输出来获取图表。您的 SG 代码已关闭。

    proc freq data=sashelp.prdsale noprint;
    table year*month / out=summary_stats;
    weight actual;
    run;
    
    proc sgplot data=summary_stats;
    series x=month y=count / group=year;
    run; 
    

    【讨论】:

      【解决方案2】:

      有多种分组、bys 或 panelbys 可以更好地传达或更好地呈现每年的计数变化。

      例如

      data have (label="Disease cases");
      
        do casedate = '01jan2006'd to '31dec2017'd;
          year = year(casedate);
          month = month(casedate);
          do day_n = 1 to 30*ranuni(123);
            case_status = floor(4*ranuni(123));
            city_id = ceil(100*ranuni(123));
            race = ceil(6*ranuni(123));
            output;
          end;
        end;
      
      run;
      
      proc means noprint data=have;
        class year month;
        types year*month;
        output out=counts n=freq;
      run;
      
      data counts;
        set counts;
        yearmo = mdy(month,1,year);
        format yearmo yymmd7.;
      run;
      
      options orientation=landscape papersize=a4;
      
      ods html close;
      ods html;
      
      proc sgplot data=counts;
        series x=month y=freq / group=year;
        xaxis type=discrete;
        where _type_ = 3;
      run;
      
      proc sgplot data=counts;
        series x=yearmo y=freq / group=year;
        xaxis type=discrete display=(novalues);
        where _type_ = 3;
      run;
      
      proc sgplot data=counts;
        by year;
        series x=yearmo y=freq / group=year;
        xaxis type=discrete ;
        where _type_ = 3;
      run;
      
      
      proc sgpanel data=counts;
        panelby year;
        series x=month y=freq ;
        colaxis type=discrete;
        where _type_ = 3;
      run;
      
      
      proc sgpanel data=counts;
        panelby year / columns=4 ;
        series x=month y=freq ;
        colaxis type=discrete display=(novalues);
        where _type_ = 3;
      run;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多