Solved
New Contributor
Posts: 2

# Question about Loops in SAS

Hello,

I am new to SAS and would like to accomplish the following task: the first one creates 100 variables, the second code piece takes the average of the 100 values (call this variable A1).

My question: I wish to repeat this 2000 times, so that I have a dataset of 2000 observations of A1, but I don't know how to modify the code...

Thanks!!

data rand;

call streaminit(-1);

do i = 1 to 100;

x = rand("Normal")*5+1;

output;

end;

run;

proc means noprint data=rand;

output out=rand (drop=_type_ _freq_)

mean(x)=A1; run;

Accepted Solutions
Solution
‎12-24-2013 04:47 AM
Occasional Contributor
Posts: 18

## Re: Question about Loops in SAS

First off, If you are taking mean(X), how can you expect 2000 values(observations) in your output A1 of proc means step. Please clarify

Did you mean -

data rand;

call streaminit(-1);

do j=1 to 2000; /*modified here, is this what you want?*/

do i = 1 to 100;

x = rand("Normal")*5+1;

output;

end;

run;

proc means noprint data=rand;

output out=rand (drop=_type_ _freq_)

mean(x)=A1; run;

Thanks,

Naveen

All Replies
Occasional Contributor
Posts: 11

## Re: Question about Loops in SAS

Hi

First thing, i did not understand the clear objective of your question. If you can give expected dataset example, it will be very much helpful.

However, as you said you are new to SAS, following is what i presume you want to do:

data new;

set rand;

do i = 1 to 2000;

output;

end;

drop i;

run;

Solution
‎12-24-2013 04:47 AM
Occasional Contributor
Posts: 18

## Re: Question about Loops in SAS

First off, If you are taking mean(X), how can you expect 2000 values(observations) in your output A1 of proc means step. Please clarify

Did you mean -

data rand;

call streaminit(-1);

do j=1 to 2000; /*modified here, is this what you want?*/

do i = 1 to 100;

x = rand("Normal")*5+1;

output;

end;

run;

proc means noprint data=rand;

output out=rand (drop=_type_ _freq_)

mean(x)=A1; run;

Thanks,

Naveen

New Contributor
Posts: 2

## Re: Question about Loops in SAS

Dear Naveen,

Thank you for the help. The first part (generating 2000 sets of 100 random values) is what I need, and I was able to solve the second part. In short, the final code would look like:

data rand;

call streaminit(-1);

do j=1 to 2000; /*modified here*/

do i = 1 to 100;

x = rand("Normal")*5+1;

output;

end;

end; /*modified here*/

run;

proc means noprint data=rand;

by j; /*modified here*/

output out=Rand_means (drop=_type_ _freq_) /*modified here*/

mean(x)=A1; run;

Kevin

Occasional Contributor
Posts: 18

## Re: Question about Loops in SAS

Hi,

It rather looks much too simple and so I'm afraid perhaps my understanding could be wrong somewhere wrong. Is this what you really want?

data rand;

call streaminit(-1);

do j=1 to 2000; /*modified here*/

do i = 1 to 100;

x = rand("Normal")*5+1;

output;

end;

end;

drop i; /*modified here*/

run;

Proc sql;

Create table Rand_means as

Select

j, mean(x) as A1

from rand

group by j;

quit;

Merry Christmas,

Naveen

🔒 This topic is solved and locked.