SAS Base (35)

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.

Check Answer
Answer: A

注解:DATA statement可以一次建立多个data set,可以通过OUTPUT statement来指定所需写入数据的data set。如果程序中没有出现OUTPUT statement,那么SAS会自动在DATA step的最后加入OUTPUT,即一下2个程序是一样的:

data d;
infile datalines;
input x;
output;
datalines;
1
;
run;
data d;
infile datalines;
input x;
datalines;
1
;
run;

本题中,出现了3个OUTPUT,第一个将SASDATA.TWO中X等于5的观测值写入SASUSER.ONE中(2个观测值),第二个将SASDATA.TWO中Y小于5的观测值写入SASUSER.TWO中(2个观测值)。第三个OUTPUT statement等价于:
OUTPUT SASUSER.ONE SASUSER.TWO OTHER;
将SASDATA.TWO中所有的数据同时写入SASUSER.ONE、SASUSER.TWO和OTHER这3个data set(3个观测值)。

SASUSER.ONE:

Obs X Y
1 5 2
2 5 2
3 3 1
4 5 6
5 5 6

SASDATA.TWO:

Obs X Y
1 5 2
2 5 2
3 3 1
4 3 1
5 5 6

OTHER:

Obs X Y
1 5 2
2 3 1
3 5 6

8 thoughts to “SAS Base (35)”

  1. SAS BASE QUESTION NO: 4 from Actualtest
    Given the SAS data set SASDATA TWO:
    SASDATA TWO
    XY
    —-
    52
    31
    56
    The following SAS program is submitted:
    data sasuser.one two sasdata.three;
    set sasdata two;
    if x = 5 then output sasuser.one;
    else output sasdata two;
    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
    Explanation:
    Notes : DATA statement can once create multiple data set, you can write data to specify the desired data set by OUTPUT statement. If the program does not appear OUTPUT statement, then the SAS DATA step will be automatically added to the final OUTPUT, that is about two programs are the same:
    data d;
    infile datalines;
    input x;
    output;
    datalines;
    1
    ;
    run; data d;
    infile datalines;
    input x;
    datalines;
    1
    ;
    run;
    In this problem , there have been three OUTPUT, the first one will SASDATA.TWO observations of X equal to 5 SASUSER.ONE in writing ( two observations ) , the second will SASDATA.TWO in Y is less than 5 observations write SASUSER.TWO in ( 2 observations ) . Third OUTPUT statement is equivalent to:
    OUTPUT SASUSER.ONE SASUSER.TWO OTHER;
    The SASDATA.TWO all data is written SASUSER.ONE, SASUSER.TWO and OTHER these three data set (3 observations ) .
    SASUSER.ONE:
    Obs X Y
    1 5 2
    2 5 2
    3 3 1
    4 5 6
    5 5 6

    SASDATA.TWO:
    Obs X Y
    1 5 2
    2 5 2
    3 3 1
    4 3 1
    5 5 6

    OTHER:
    Obs X Y
    1 5 2
    2 3 1
    3 5 6

  2. Why are the observations outputted to SASUSER.ONE and SASUSER.TWO in the order shown above?
    e.g. Why is SASUSER.ONE not outputted as following (2 obs with x=5 shown first then the obs from the input dataset)?
    SASUSER.ONE:
    Obs X Y
    1 5 2
    2 5 6
    3 5 2
    4 3 1
    5 5 6

    Thanks!

    1. As SASDATA.TWO has 3 records, the program will run 3 times.
      In the first iteration, X is equal to 5, so the first IF statement outputs 5 to SASUSER.ONE. After that, the OUTPUT statement, which is equivalent to OUTPUT SASUSER.ONE SASUSER.TWO OTHER, adds another 5 into SASUSER.ONE.

      In the second iteration, as X is not equal to 5, the first IF statement does nothing, and the OUTPUT statement adds 3 into SASUSER.ONE.

      Third iteration does the same as the first iteration, adding two 5s.

      Therefore, the order of X in SASUSER.ONE is 55355.

  3. Run the following program PROGRAM RUN:
    libname SASDATA “C:\Users\C62738\Downloads”;
    libname SASAV “C:\Users\C62738\Downloads”;
    data SASDATA.TWO;
    input X Y;
    datalines;
    5 2
    3 1
    5 6
    data SASAV.ONE SASAV.TWO OTHER;
    set SASDATA.TWO;
    if X eq 5 then output SASAV.ONE;
    if Y lt 5 then output SASAV.TWO;
    output;
    run;
    proc print data=SASDATA.TWO;
    title “SASDATA.TWO “;
    run;
    proc print data=SASAV.ONE;
    title “SASAV.ONE “;
    run;
    proc print data=SASAV.TWO;
    title ” SASAV.TWO “;
    run;
    proc print data=OTHER;
    title ” OTHER “;
    run;

    There were 3 observations read from the data set SASDATA.TWO.
    The data set SASAV.ONE has 5 observations and 2 variables.
    The data set SASAV.TWO has 5 observations and 2 variables.
    The data set WORK.OTHER has 3 observations and 2 variables.

Leave a Reply

Your email address will not be published. Required fields are marked *