Input statement inside a do loop with and without trailing @@ illustration help?

Reply
Frequent Contributor
Posts: 137

Input statement inside a do loop with and without trailing @@ illustration help?

Hi,

I request your valuable time in explaining the application of Input statement in a do loop with one simple and another medium complex example to help  me understand the concept. The reason why I am asking is because, despite reading the documentation I am not comprehending the subject very well.  Also, the documentation being so long (with too many options), by the time I got through till the end of it, I seem to lose my way.

I'd appreciate if you could illustrate your example with a single trailing @ and double trailing @@ in the do loop. Thanks for the favor!.

Charlotte

Esteemed Advisor
Posts: 5,198

Re: Input statement inside a do loop with and without trailing @@ illustration help?

To give you relevant input, please describe what issue drives you to use this concept. Chances are otherwise that our explanation will not make it clearer for your...

Data never sleeps
Respected Advisor
Posts: 3,775

Re: Input statement inside a do loop with and without trailing @@ illustration help?

The double trailing at is to hold the INPUT line for the next iteration of the "data step loop" sometime called "observation loop".

Ruminate on that for a bit.

Frequent Contributor
Posts: 137

Re: Input statement inside a do loop with and without trailing @@ illustration help?

Hi John, Thanks, Yeah I got that. so If there is a dataline like:

123456

input x @@; *will output like

1

2

3 * hope my understanding is right?

Now, my difficulty in understanding lies when such input statement are used in a do loop like

do i= 1 to some_number(or some variable);

input x @@;

end;

This is where I am stumped. It's doesn't seem as easy as understanding the use set statement in a do loop. Sorry for the bother

Respected Advisor
Posts: 3,775

Re: Input statement inside a do loop with and without trailing @@ illustration help?

You can do your own testing and you will learn more.  Run this and consider why it gives 80 obs.

data atat;
   input x $1. @@;
   cards;
123456
;;;;
   run;
Respected Advisor
Posts: 3,775

Re: Input statement inside a do loop with and without trailing @@ illustration help?

Now try this version.

data at;
   infile cards eof=eof;
   input x $1. @;
   do while(not missing(x));
      output;
     
input x $1. @;
      end;
eof:
stop;  
  
cards;
123456
;;;;
   run;
Frequent Contributor
Posts: 137

Re: Input statement inside a do loop with and without trailing @@ illustration help?

John wow, That's a brain teaser. You are giving me true blonde momentsSmiley Sad

1. Result of the 1st code tests:

I experimented running them again and again. Then I inserted a space between the letters and removed the informat. This seem to just output 6 obs. Still lost in the logic why 80obs, although it seems it's got something to do the position of the cursor?

2. Result of 2nd code: very neat

/*first input gets the record into the input buffer, and does it hold there until the 2nd input executes? but the explicit output should override the implicit output before step boundary is reached right, oh my god I am knocked out*./

Trusted Advisor
Posts: 1,300

Re: Input statement inside a do loop with and without trailing @@ illustration help?

cards is unbuffered, 80 obs because line length is fixed for cards at 80...

another--

data foo;

input x $1. @@;

if missing(x) then stop;

cards;

123456

;

run;

Message was edited by: FriedEgg for correctness

Frequent Contributor
Posts: 137

Re: Input statement inside a do loop with and without trailing @@ illustration help?

HI Matt/John, Very very Interesting. thank you so much for your time, nevertheless, like you said I will have to test over and over to get my understanding bingo. It's 11pm here in Liverpool, England and I am so knackered that my eyes are barely open. I will put my best effort tomorrow and get back to you should I still find it difficult to understand.

Respected Advisor
Posts: 3,775

Re: Input statement inside a do loop with and without trailing @@ illustration help?

Actually CARDS data is un-buffered which is why you can't use INFILE statement option END.  The default record length is fixed that is why the example program reads 80 columns to EOF.

data _null_;
  
infile cards end=eof;
   input;
  
list;
  
cards;
123456
;;;;
   run;

32         data _null_;
33            infile cards end=eof;
WARNING: The value of the INFILE END= option cannot be set for CARDS or DATALINES input.
34            input;
35            list;
36            cards;

RULE:      ----+----
1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0                    
37         123456
Respected Advisor
Posts: 3,775

Re: Input statement inside a do loop with and without trailing @@ illustration help?

Let's change #2 slightly to illustrate how single trailing at works for this example with multiple records.

data at;
   infile cards eof=eof;
   id + 1;
  
input x $1. @;
   do while(not missing(x));
      output;
     
input x $1. @;
      end;
  
return;
eof:
stop;  
  
cards;
123456
65432 Notice-this-bit-not-read.
654
;;;;
   run;

** What will happen if you use @@ above.
Frequent Contributor
Posts: 137

Re: Input statement inside a do loop with and without trailing @@ illustration help?

Good morning John   /Matt/Tom, Thank you so very much for your time and patience. It seems to me that before I ask any follow up question and irritate you, let me continue testing your code reading between the statements in detail and get some understanding, so that my questions will sound wiser. Please forgive me. I really am trying my level best to not ask to be spoon fed or annoy you. I'm rather irritated with myself. I will keep this thread open for another day or two and will come back to you with relatively good questions having some level of home work done.

Have a nice day,

Charlotte

SAS Super FREQ
Posts: 8,719

Re: Input statement inside a do loop with and without trailing @@ illustration help?

Hi, I know I'm late to this party, but I ran across these examples of a double trailing @ (@@) in a SAS/STAT example and thought I would post it here for you.

Cynthia

http://support.sas.com/documentation/cdl/en/statug/67523/HTML/default/viewer.htm#statug_logistic_exa...

do loop with input and @@ example with PROC LOGISTIC (basically, they are reading and rereading the input file to make the number of cases they need for the procedure)

and here's an example without a DO loop Base SAS(R) 9.4 Procedures Guide: Statistical Procedures, Third Edition where 4 variables for Iris data are read over and over again out of the input file (sometimes people have this type of data, where "pairs" of measurements are automatically added to a file by some kind of device or process. In this case, every 4 data points in the input file belong to 1 observation. So if you run the code you will get 50 obs because there are 50 "sets" of 4 datapoints read to build each obs.

Respected Advisor
Posts: 3,775

Re: Input statement inside a do loop with and without trailing @@ illustration help?

Your first example is not very good because the the data step reads all the data in one iteration.    @ would suffice.

The second example is classic @@.

Super User
Super User
Posts: 6,332

Re: Input statement inside a do loop with and without trailing @@ illustration help?

You can think of SAS as having a column pointer and a line pointer.

An input statement without any trailing @ or @@ will increment the line pointer and set the column pointer to 1.

An input statement with @@ also will prevent SAS from doing the same at the end of the data step.

DO loop really doesn't have much to do with understanding how it works.  Consider that you could always just code a do loop as a series of statements.

do i=1 to 3 ; input x @ ; output; end;

Is the same as

input x @; output;

input x @; output;

input x @; output;

Ask a Question
Discussion stats
  • 14 replies
  • 405 views
  • 0 likes
  • 6 in conversation