DATA Step, Macro, Functions and more

DO LOOP

Reply
Super Contributor
Posts: 1,041

DO LOOP

Hi Team,

I did not understand how to interpret this small program. Could someone help me.

data a;

do A=0,1;

do B=0,1;

do Y=1,2;

input F @@;

output;

end;

end;

end;

datalines;

23 63 31 70 67 100 70 104

;

run;

Output is:

A   B   Y   F

0   0    1   23

0   0    2   63

0   1    1   31

0   1     2   70

1   0     1    67

1   0    2     100

1    1    1     70

1     1   2    104

Thanks

PROC Star
Posts: 7,492

Re: DO LOOP

Posted in reply to robertrao

It is doing exactly what the program is telling it to do.

When  A=0 and B=0 and Y=1 it is inputting the first value (23)

it continues to read the same record because of the @@ included in the input statement.

Then when A=0 and B=0 and Y=2 it reads the second record

Then when A=0 and B=1 and Y=1 it reads the third record,

etc., etc., etc.

Super Contributor
Posts: 1,041

Re: DO LOOP

Thanks so much for the explanation.

Regards

Frequent Contributor
Posts: 86

Re: DO LOOP

Posted in reply to robertrao

It seems to me like a cartesian Product.

Super User
Super User
Posts: 7,076

Re: DO LOOP

Posted in reply to robertrao

It is a short hand way to enter data when the design factors (A,B,Y) are fixed.  Only the value of F needs to be in the input data as the relative order determines what value of A,B,Y goes with the particular value of F.

Things that this program uses that beginners might not understand :

Use of trailing @@ on INPUT statement.  This is not strictly needed in the example program because all of the data is on one line so that a single at sign would also work.  But the double at sign allows you to split the data across rows when you have more than can fit on a single line of the input program.  For example in this program you can split the input data into two or more lines and get the same result.

Use of DO loop with a list of values.  In fact with a DO loop in SAS you can actually have in more complex list of values.  You can have :

do i=1 to 5,10,15 ;

And it will run the loop 7 times with I being set to 1,2,3,4,5,10,15 .

Use of the OUTPUT statement.  This will cause it to generate an observation in the output dataset.  So in this case one iteration of the data step will generate 2*2*2=8 output observations.

Super Contributor
Posts: 1,041

Re: DO LOOP

Thanks so much for the details  to understand the topic

Regards

Ask a Question
Discussion stats
  • 5 replies
  • 278 views
  • 0 likes
  • 4 in conversation