SAS讲义-第九课
一、Do循环
  1 大家回看第四课的例11,可以发现Do循环应该要和End搭配使用。下面都是可行的Do语句。
do while语句怎么用    do i=5;
    do i=2,3,5,7;
    do i=1 to 100;
    do i=1 to 100 by 2;
    do i=100 to 1 by -1;
    do i=1 to 5,7 to 9;
    do i=’01jan99’d,’25feb99’d;
    do i=’01jan99’d to ‘01jan2000’d by 1;
1 产生1,2,9,8 的序列。
data a;
do i=1,2,9,8;
output;
end;
run;
    思考:若output放在end之后,或者去掉output,那会怎样呢?
2 产生1-20的奇数序列。
data a;
do i=1 to 20 by 2;
output;
end;
run;
3 1-100的自然数之和。
data a;
do i=1 to 100 ;
n+i;
output;
end;
run;
4 1-100的自然数的平方和。
data a;
do i=1 to 100 ;
n+i**2;
output;
end;
run;
5do循环处理数组。(下课还会深入说数组)
data a(drop=i);
array day{7} d1-d7;
do i=1 to 7;
day{i}=i+1;
end;
run;
    2do while语句。先判断while表达式,若成立则执行,否则推测循环。
6
data a;
n=0;
do while (n<5);
n+1;
output;
end;
run;
7 计算1加到100的过程中,第一个大于等于2000的数。
data a;
do i=1 to 100 while (n<2000) ;
n+i;
output;
end;
run;
    3do until 语句。先执行,直到until的表达式为真,推出循环。
    4do over 语句。我们到下课再说。
二、select语句。
    Select-when 相当于一般编程语言里面的swich-case语句。直接看例子。
8 data a;
set resdat.class;
x=0;
obs=_n_;
select(obs);
when(2) x=2;
when(3,7)x=5;
otherwise x=3;
end;
run;
三、return语句。
    Return语句可以让系统返回到data步开头。
9return语句与if-then共用
data a;
input x y z;
if x=y then return;
s=x+y;
cards;
1 2 3
2 2 3
;
run;
    分析:默认情况下,系统将每条观测读入到数据集a,可是当x=y时,return语句被执行,也就是s=x+y没有被执行,那么s即为空值。思考,如果在s=x+y语句后面增加一条output,又会怎样呢?此时要考虑一旦有了outputrun本身的输出功能消失了。
10 return语句与Do循环语句共同使用。
data a;
input a b c @@;
do x=1 to 5;
ax=a*x;
if ax>b then return;
output;
end;
cards;
1 2 3 2 6 8
;
run;
    分析:由于Do循环包含了output,一个输入数据行本来可以生产5条观测,可是在循环中增加了return。也就是,当ax>b,系统执行return语句,没有执行output语句。
练习10某人将500元存入在某银行的存款账户,这个账户利率为7%,每年计息一次。用累加语句和循环语句计算三年末此人能得多少钱。
下面的练习仅供大家参考学习,不作练习。
练习11 given the SAS data set SASDATA.TWO:
  X  Y
  -- --
  5  2
  3  1
  5  6
The following SAS program is submitted:
  data SASUSER.ONE  SASUSER.TWO  OTHER;
    set SASDATA.TWO;
    if X eq 5 then output SASUSER.ONE;
    if Y lt 5 then output SASUSER.TWO;
    output;
  run;
What is the result?
    A.
data set SASUSER.ONE has 5 observations   
data set SASUSER.TWO has 5 observations
data set WORK.OTHER  has 3 observations
    B.
data set SASUSER.ONE has 2 observations   
data set SASUSER.TWO has 2 observations
data set WORK.OTHER  has 1 observations
    C.
data set SASUSER.ONE has 2 observations   
data set SASUSER.TWO has 2 observations
data set WORK.OTHER  has 5 observations
    D. No data sets are output. The DATA step fails execution due to syntax errors.
Answer: A
练习12Consider the following data step:
  data WORK.NEW;
    set WORK.OLD(keep=X);
    if X < 10 then X=1;
    else if X >= 10 AND X LT 20 then X=2;
    else X=3;
  run;
In filtering the values of the variable X in data set WORK.OLD, what new value would be assigned to X if its original value was a missing value?
    A. X would get a value of 1.
    B. X would get a value of 3.
    C. X would retain its original value of missing.
    D. This step does not run because of syntax errors.
Answer: A
练习 13 The following SAS program is submitted:
  data WORK.SALES;
    do Year=1 to 5;
        do Month=1 to 12;
          X + 1;
        end;
    end;
  run;
How many observations are written to the WORK.SALES data set?
    A. 0
    B. 1
    C. 5
    D. 60
Answer: B /*假如在X+1后添加output,那又选什么?*/
练习14 The following SAS program is submitted:
  data WORK.OUTDS;
    do until(Prod GT 6);
        Prod + 1;
    end;
  run;
What is the value of the variable Prod in the output data set?
    A. . (missing)
    B. 6
    C. 7
    D. Undetermined, infinite loop.
Answer: C
练习15 Given the SAS data set WORK.PRODUCTS:
  ProdId    Price    ProductType    Sales    Returns
  ------    -----    -----------    -----    -------
  K12S      95.50    OUTDOOR          15          2
  B132S      2.99    CLOTHING        300        10
  R18KY2    51.99    EQUIPMENT        25          5
  3KL8BY    6.39    OUTDOOR          125        15
  DY65DW    5.60    OUTDOOR          45          5
  DGTY23    34.55    EQUIPMENT        67          2
The following SAS program is submitted:
  data WORK.REVENUE(drop=Sales Returns Price);
    set WORK.PRODUCTS(keep=ProdId Price Sales Returns);
    Revenue=Price*(Sales-Returns);
  run;
How many variables does the WORK.REVENUE data set contain?
    A. 2

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。