Home Forums Main Forums SAS Forum Trailing @ and @@ in INPUT statement in SAS

  • Trailing @ and @@ in INPUT statement in SAS

     Datura updated 4 years ago 1 Member · 1 Post
  • Datura

    Member
    November 14, 2020 at 9:53 am

    In SAS DATA step, INPUT statement is used to read external data into SAS. Many people are confused about the uses of trailing @ and double @@ with INPUT statement. Let me explain them in details today.

    By default, each INPUT statement in a DATA step reads a new data record into the input buffer. However, sometimes the input data is quite complicated and we need to allow the next INPUT statement to read from the same record. Therefore we need to use @ or @@ to achieve it, which are called Line-Hold Specifiers.

    Case 1: Single trailing @

    Single trailing @ holds an input record for the execution of the next INPUT statement within the same iteration of the DATA step. It is useful when you need to read from a record multiple times with multiple INPUT statements, which holds the current record until next INPUT, but cannot hold it across DATA step iterations.

    For example, given below data, a record could be either course/professor information or student and score information. We need to read in the first variable Type and check on it, then decide what variables to use with the next INPUT statement, therefore we use the “input Type $ @ ” first. If we do NOT use the trailing @ with it, the next INPUT statement in the IF-ELSE condition will read next record automatically. If so, we can never read in the Professor and Course information. Under this circumstance, we need to use the trailing @ to hold the current record for the next INPUT statement so that we read in the Professor and Course information. Please note: we use RETAIN statement to retain Professor and Course information across iterations.

    data AAA(drop=Type);
    retain Professor Course;
    input Type $ @;
    if Type='P' then input Professor $ Course $;
    else do;
    input Student $ Score ;
    output;
    end;
    cards;
    P George 3A03
    S Alice 93
    S Nathan 81
    P Smith 4C06
    S Steve 76
    ;
    run;

    Below gives the SAS output:

    Case 2: Double trailing @@

    In some cases, each data line may contain multiple observations, thus we need to create multiple observations from each input record. In this situation, we can use double trailing @@ to accomplish it. The double @@ not only holds the current input record for next INPUT, but also holds it across DATA step iterations until the end of the data line. For example, below code will read in all the data and yield 6 observations.

    data BBB;
    input ID Name $ Age @@;
    cards;
    101 Alice 27 102 Peter 38 103 Susie 22 104 Norah 31
    105 Lucy 19 106 Esther 43
    ;
    run;

    In summary, a single trailing @ will hold the current record until next INPUT statement, it cannot hold it across DATA step iterations. However, the double trailing @@ can hold the current record across DATA step iterations, until reaching the end of the record line. Please note: Both the single and double trailing @ must be the last item in the INPUT
    statement.

    Any questions?

    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.
    • This discussion was modified 4 years ago by  Datura.

Log in to reply.

Original Post
0 of 0 posts June 2018
Now