Help using Base SAS procedures

SAS @ in data

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

SAS @ in data

Hi,

Basically, I am trying to figure out two things from the code below:

1. Using @ after num in the input holds num or id when reading the rest of line.

2 Why I get different results when I use input num @ after the do while than if I use input num. I get expected results when using @ but still would be helpful if someone could tell why can not I avoid using @.

Data test;
infile datalines Missover;
input id num @; /*this holds the current num record*/
nump = 0; /*initiate nump*/
do while (num ne .);
  nump + 1 ; /* increment nump by 1 if num is ne .*/
  output;
input num @;
end;
datalines;
100 1 2 3
101 4 5
102 6 7 8 9 10
;

proc print;run;


Accepted Solutions
Solution
‎08-14-2013 07:35 PM
Super User
Super User
Posts: 6,500

Re: SAS @ in data

You need the @ to continue to read from the same line.

Note that in general you should use TRUNCOVER instead of MISSOVER, but since you are using list mode input it will not matter in this program.

What happens when there are no values for NUM?  Your program will not output any observation for such a line.  You might want to change from WHILE () to UNTIL () so that you will get one observation with NUM=. .

You can have the DO loop do the counting for you.

do nump=1 by 1 until (num ne .);

  output;

  input num @;

end;

View solution in original post


All Replies
Respected Advisor
Posts: 4,649

Re: SAS @ in data

With the @ in the loop, you are always reading from the same line. But without the @, the first time you go through the loop, you are reading the same line as the first input statement because it ended by @. The second time you execute the loop, in the absence of a trailling @ you are reading the next line, because the last input statement didn't end with a @. 

hth

PG

PG
Solution
‎08-14-2013 07:35 PM
Super User
Super User
Posts: 6,500

Re: SAS @ in data

You need the @ to continue to read from the same line.

Note that in general you should use TRUNCOVER instead of MISSOVER, but since you are using list mode input it will not matter in this program.

What happens when there are no values for NUM?  Your program will not output any observation for such a line.  You might want to change from WHILE () to UNTIL () so that you will get one observation with NUM=. .

You can have the DO loop do the counting for you.

do nump=1 by 1 until (num ne .);

  output;

  input num @;

end;

Respected Advisor
Posts: 4,649

Re: SAS @ in data

You mean

until (num eq .)

PG

PG
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 208 views
  • 7 likes
  • 3 in conversation