## how to use a loop in a loop

Solved
Frequent Contributor
Posts: 122

# how to use a loop in a loop

Hi, I wrote a big macro to execute a do loop. While in the loop, I want to write another loop to do a job. I am not familiar how to do this. In the second loop, I have a dataset "have" to start with. In the dataset, I have four variables, firm, assets, parm1 and parm2. It looks like below

dataset have

firm          asset          parm1          parm2

01               50               0.2               10

02               30               0.2               15

03               10               0.1               20

...

Firms are unique. For each firm, I want to create 10 observations. Each observation is equal to parm1**(i)*(asset-parm2). For example, for firm 01, p1(the first observation) = 0.2**(1) * (50-10), p2(the second obs) = 0.2**(2)*(50-10), ... p10 = 0.2**(10)*(50-10).

So the dataset I want to obtain is like

firm          p          asset          parm1          parm2

01          p1          50               0.2               10

01          p2          50               0.2               10

...

01          p10 .....

02          p1

...

...

Please advise how to write a do loop either in proc sql or in data to achieve this purpose.

Accepted Solutions
Solution
‎03-09-2014 11:14 AM
Super User
Posts: 8,120

## Re: how to use a loop in a loop

Does not sound like macro has anything to do with want you want. This can be done with a simple DO loop in a data step.

data want ;

set have ;

do i=1 to 10 ;

p=parm1**(i)*(asset-parm2);

output;

end;

run;

All Replies
Posts: 4,736

## Re: how to use a loop in a loop

I believe it would help if you could provide us with a bit more background and eventually also with your macro. Not sure at all what you're trying to do here but may be Proc Plan could be interesting for you.

Solution
‎03-09-2014 11:14 AM
Super User
Posts: 8,120

## Re: how to use a loop in a loop

Does not sound like macro has anything to do with want you want. This can be done with a simple DO loop in a data step.

data want ;

set have ;

do i=1 to 10 ;

p=parm1**(i)*(asset-parm2);

output;

end;

run;

🔒 This topic is solved and locked.