SAS求和一个特定的值

SAS求和一个特定的值,sas,Sas,我有一个数据集,其中包含特定月份的每一天的值,如01JAN2020、03JAN2020、06JAN202、01FEB2020、04FEB2020。我需要计算特定月份和年份的行数。当我使用count*函数并按特定列对其进行分组时,我只收到每天的行计数。哪个函数将显示特定月份的行数总和,而不是每日总和 谢谢,在SQL中,为了计算日期月份的合计计数,GROUP BY应为日期月份。可以使用INTNX函数或年和月函数计算月或月的第一天 过程SQL 例如: data have; call streami

我有一个数据集,其中包含特定月份的每一天的值,如01JAN2020、03JAN2020、06JAN202、01FEB2020、04FEB2020。我需要计算特定月份和年份的行数。当我使用count*函数并按特定列对其进行分组时,我只收到每天的行计数。哪个函数将显示特定月份的行数总和,而不是每日总和


谢谢,

在SQL中,为了计算日期月份的合计计数,GROUP BY应为日期月份。可以使用INTNX函数或年和月函数计算月或月的第一天

过程SQL

例如:

data have;
  call streaminit(2021); * initialize random number stream;

  do date = '01jan2020'd to today();
    do _n_ = 1 to rand('integer', 5);  * random, up to 5 repeats per day;
      output;
    end;
  end;

  format date date9.;
run;

proc sql; 
  create table want as
  select 
    intnx('month', date, 0) as month format=yymon7.
  , count(*) as count
  from
    have
  group by
    calculated month /* calculated is SAS SQL special feature */
  ;

proc means nway data=have noprint ;
  format date yymon7.;
  class date;
  var date;
  output out=want N=count;
run;
过程意味着

您还可以使用Proc MEANS并将日期格式化为月份表示形式。该过程将根据格式化的值进行分组

例如:

data have;
  call streaminit(2021); * initialize random number stream;

  do date = '01jan2020'd to today();
    do _n_ = 1 to rand('integer', 5);  * random, up to 5 repeats per day;
      output;
    end;
  end;

  format date date9.;
run;

proc sql; 
  create table want as
  select 
    intnx('month', date, 0) as month format=yymon7.
  , count(*) as count
  from
    have
  group by
    calculated month /* calculated is SAS SQL special feature */
  ;

proc means nway data=have noprint ;
  format date yymon7.;
  class date;
  var date;
  output out=want N=count;
run;

PROC FREQ+用于显示日期的格式。第一个示例是按年份-月份,第二个示例只是按月份名称

   *by year month;
   proc freq data=sashelp.stocks;
   format date yymmn6.;
   table date;
   run;
   
   *by month name;
   proc freq data=sashelp.stocks;
   format date monname.;
   table date;
   run;