Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create new variable with do loop 1

Status
Not open for further replies.

chris93

Programmer
Jul 24, 2011
5
US
btw, I tried it this way, but then the new variable s would be 3 for the entire column.

data iris5;
set iris1;
do i=1 to 150;
if MOD(i,3)= 1 THEN s=1;
else if MOD(i,3)= 2 THEN s=2;
else if MOD(i,3)= 0 THEN s=3;

end;
run;
 
Hi Chris,

You almost have it with your last row, but why the looping?

if mod(i,3) = 1 then reset, otherwise add 1

Code:
data iris1;
input m1-m4 @@;

count ++ 1;
if mod(_n_,3) = 1 then count = 1;

cards;
5.1  3.5  1.4  0.2  7.0  3.2  4.7  1.4  6.3  3.3  6.0  2.5
4.9  3.0  1.4  0.2  6.4  3.2  4.5  1.5  5.8  2.7  5.1  1.9
4.7  3.2  1.3  0.2  6.9  3.1  4.9  1.5  7.1  3.0  5.9  2.1
4.6  3.1  1.5  0.2  5.5  2.3  4.0  1.3  6.3  2.9  5.6  1.8
5.0  3.6  1.4  0.2  6.5  2.8  4.6  1.5  6.5  3.0  5.8  2.2
5.4  3.9  1.7  0.4  5.7  2.8  4.5  1.3  7.6  3.0  6.6  2.1
run;

proc print;run ;

If you want you can loop it with the set statement in the loop. The logic can appear clearer this way (at least in my opinion).

Code:
data test ;
   do count = 1 to 3 ;
      set iris1 ;
      output ;
      end ;
   run ;
proc print;run;

HTH
 
this looks really interesting,

I am still trying to figure things out with SAS , and making the transition from C++, it appears that SAS do loops works somehow different than C++ for loops. Are SAS Do loops always within the Data Step?


Also what does count ++ 1; stand for ?

Is it the same as a for loop like:

for count 1 to ? (I am not sure what value to put for the last iteration)


For the second loop,
I tried it wihout the output statement and it appears that SAS would only only give every third observation from iris1 while count is 4 for each observation.


data test ;
do count = 1 to 3 ;
set iris1 ;

end ;
run ;
proc print;run;
 
Hi Chris,

As far as I know, the SAS do loop is comparable to the C for loop. Syntatically it is a bit different.

For eg. The C for loop:

Code:
	#include<stdio.h>

	int main()
	{
     		int i;
     		for (i = 0; i < 10; i++)
     		{
          		printf ("Hello\n");
         		printf ("World\n");
     		}
     	return 0;
	}

is comparable to the following SAS do loop:

Code:
data _null_  ;
   do i = 1 to 10 ;
      put "Hello";
      put "World";
      end ;
   run ;

SAS has a datastep do loop and a macro do loop that behave slightly differently.

The main difference is that the datastep allows for combinations of different types of loops for more complex conditions.

Here is a rather contrived example

Code:
*Print all integers whose sum does not exceed 20 ;
data _null_  ;
   do i = 1 to 10 while (tot < 20) ;
      put i= tot= ;
	  tot ++ i ;
	  end ;
   run ;

output:

i=1 tot=0
i=2 tot=1
i=3 tot=3
i=4 tot=6
i=5 tot=10
i=6 tot=15

The count++1 syntax is just an incrementer variable that is incremented by 1 in this example. Count +1 will do the same. I use two + so it stands out in the code so I can easily identify where the incrementation is happening. It is comparable to the count+=1 shorthand of C.

Regarding your last question. SAS has an implicit loop (that is literally what the datastep is) that cycles through each record of the datastep. There is also an implicit output statement before the run statement. Using an explicit output in the code overrides the implicit output statement.

In your example, you are explicitly looping through the dataset iris1. If you follow the logic, we are reading 3 rows from the dataset in the explicit loop. By the time we get to the run statement, only the last record will be in the PDV (Program Data Vector - Think of it as an array that contains the last row) which is output.

I hope this clarifies things a bit better. There are a lot of differences between a specialised language like SAS and a general programming language like C, but I think once you have a solid grounding on how the PDV works, then the rest should be fairly straight-forward.

I would strongly recommend reading this paper.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top