Proc SAS在SAS中执行循环

时间:2017-07-12 01:11:19

标签: sas proc-sql do-loops

对于作业,我被要求在Proc Sql语句中创建一个do循环。我的程序没有识别m1sales和m2sales。这是我必须创建的数据集和宏。第一个宏是设置允许人们将qtr设置为数字。第二个宏根据月份设置第一,第二和第三个月。第一个proc sql正在做我想要的。第二个不是我添加do语句。我得到了额外的声明,“%do循环不在'create table命令中。%do循环被替换为create table命令后面的一系列文本;该文本可能是'在'内'创建表语句。“任何人都可以告诉我如何正确创建循环吗?

data Month1;
input Name $ sales;
cards;
Joyce 235
Marsha 352
Bill 491
Vernon 210
Sally 418
;
data Month2;
input Name $ sales;
cards;
Joyce 169
Marsha 281
Bill 315
Vernon 397
Sally 305
;
data Month3;
input Name $ sales;
cards;
Joyce 471
Marsha 314
Bill 394
Vernon 291
Sally 337
;
data Month4;
input Name $ sales;
cards;
Joyce 338
Marsha 259
Bill 310
Vernon 432
Sally 362
;
data Month5;
input Name $ sales;
cards;
Joyce 209
Marsha 355
Bill 302
Vernon 416
Sally 475
;
data Month6;
input Name $ sales;
cards;
Joyce 306
Marsha 472
Bill 351
Vernon 405
Sally 358
;

options symbolgen;
%Macro quarter(quarter);
%Global qtr;
%Let qtr = &quarter;
%Mend quarter;
%quarter (1);

options mprint symbolgen;
%Macro Month(day);
%Global One;
%Global Two;
%Global Three;
%if &qtr = %eval(1) %then %do; 
    %Let One = 1;
    %Let Two = 2;
    %Let Three = 3;
    %end;
%Else %if &qtr = %eval(2) %then %do; 
    %Let One = 4;
    %Let Two = 5;
    %Let Three = 6;
    %end;
%Else %if &qtr = %eval(3) %then %do; 
    %Let One = 7;
    %Let Two = 8;
    %Let Three = 9;
    %end;
%Else %if &qtr = %eval(4) %then %do; 
    %Let One = 10;
    %Let Two = 11;
    %Let Three = 12;
    %end;   
%Mend Month;
%Month(&qtr);

正确的代码:

%Macro qtrearn(x);
proc sql;
create table qtr&x as
select Month&One..name, month&One..sales as m&One.sales, month&Two..sales as m&Two.sales,
  month&Three..sales as m&Three.sales, sum(month&One..sales, month&Two..sales, month&Three..sales) as qtr&x.sales
  from month&One, month&Two, month&Three
  where month&One..name=month&Two..name=month&Three..name;
select sum(m&One.sales) as m&One.total, sum(m&Two.sales) as m&Two.total, sum(m&Three.sales) as m&Three.total,
  sum(qtr&x.sales) as qtr&x.total
  from qtr&x;
%Mend qtrearn;
%qtrearn(&qtr);

不使用do循环的代码。我需要为赋值插入do循环。

options mprint symbolgen;
%Macro qtrearn(x);
proc sql;
%do i = &One %to &Three;
create table qtr&x as
select Month&i..name, month&&i..sales as m&&i.sales,
  sum(month&One..sales, month&Two..sales, Month&Three..sales) as qtr&x.sales
  from month&One, month&Two, month&Three
  where month&One..name=month&Two..name=month&Three..name;
%end;
select sum(m&One.sales) as m&One.total, sum(m&Two.sales) as m&Two.total, sum(m&Three.sales) as m&Three.total,
  sum(qtr&x.sales) as qtr&x.total
  from qtr&x;

%Mend qtrearn;
%qtrearn(&qtr);

1 个答案:

答案 0 :(得分:0)

我能够解决它。万一其他人想要它。这是答案:

%Macro qtrearn(x);
proc sql;
create table qtr&x as
select Month&One..name, 
  %do i = &One %to &Three;
  month&&i..sales as m&&i.sales,
  %end;
  sum(month&One..sales, month&Two..sales, month&Three..sales) as qtr&x.sales
  from month&One, month&Two, month&Three
  where month&One..name=month&Two..name=month&Three..name;
create table totals_qtr&x as
select   %do i = &One %to &Three;
  sum(m&&i.sales) as m&&i.total,
  %end; 
  sum(qtr&x.sales) as qtr&x.total
  from qtr&x;
proc print data=work.qtr&x;
run;
proc print data=work.totals_qtr&x;
run;
%Mend qtrearn;
%qtrearn(&qtr);