DATA Step, Macro, Functions and more

Why answer is this?

Reply
New Contributor
Posts: 2

Why answer is this?

Given the text file COLORS.TXT:

----+----1----+----2----+----

RED    ORANGE  YELLOW  GREEN
BLUE   INDIGO    PURPLE   VIOLET
CYAN   WHITE     FUCSIA    BLACK
GRAY   BROWN   PINK        MAGENTA

The following SAS program is submitted:

data WORK.COLORS;
    infile 'COLORS.TXT';
    input @1 Var1 $ @8 Var2 $ @;
    input @1 Var3 $ @8 Var4 $ @;
run;

What will the data set WORK.COLORS contain?

 

Answer:

Var1         Var2               Var3        Var4
------          ------              ------         ------
RED         ORANGE      RED         ORANGE
BLUE        INDIGO        BLUE       INDIGO
CYAN        WHITE         CYAN       WHITE
GRAY        BROWN       GRAY       BROWN

Super User
Posts: 10,599

Re: Why answer is this?

Posted in reply to mohitraj4u

The trailing @ holds the record pointer in the current line, so the second input statement re-reads positions 1 and 8; the third and fourth column of the infile are therefore never read.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 24,026

Re: Why answer is this?

Posted in reply to mohitraj4u

Look at the INPUT statement. You are using both @ to denote the position of where to start reading and a trailing @. 

 

The trailing @ keeps the pointer at the line for that data step iteration. At the next data step iteration it moves on. Check the behaviour by running the code with and without the trailing @. 

 

    input @1 Var1 $ @8 Var2 $ @; <- reads var1 starting from position1, var2 from position 8 and holds the line.
    input @1 Var3 $ @8 Var4 $ @; <- reads var3 starting from position1, var4 from position 8  and holds the line. The run allows it to move to a new line. 
 

mohitraj4u wrote:

Given the text file COLORS.TXT:

----+----1----+----2----+----

RED    ORANGE  YELLOW  GREEN
BLUE   INDIGO    PURPLE   VIOLET
CYAN   WHITE     FUCSIA    BLACK
GRAY   BROWN   PINK        MAGENTA

The following SAS program is submitted:

data WORK.COLORS;
    infile 'COLORS.TXT';
    input @1 Var1 $ @8 Var2 $ @;
    input @1 Var3 $ @8 Var4 $ @;
run;

What will the data set WORK.COLORS contain?

 

Answer:

Var1         Var2               Var3        Var4
------          ------              ------         ------
RED         ORANGE      RED         ORANGE
BLUE        INDIGO        BLUE       INDIGO
CYAN        WHITE         CYAN       WHITE
GRAY        BROWN       GRAY       BROWN



Ask a Question
Discussion stats
  • 2 replies
  • 175 views
  • 0 likes
  • 3 in conversation