DATA Step, Macro, Functions and more

Data input Array with nested do loop

Accepted Solution Solved
Reply
Highlighted
Contributor
Posts: 30
Accepted Solution

Data input Array with nested do loop

Good afternoon trying to get 2 variables cholestor_level and treat and ideally do this in 1 data step vs 2.  Have been fooling around with this for a while without luck trying nested do loop in this iteration but was initially thinking a 2 dimensional array should work but I could not figure that out either.  The fact that there is no treat variable in the data set is really confusing me.  This attempt is producing 90 observations in the output vs the expected 30 and is assigning every cholesterol_level to each treatment.     

 

DATA cholestorol_input;

input C1-C10;

CARDS;

 

220 190 180 185 210 170 178 200 177 189

160 168 178 200 172 155 159 167 185 199

240 220 246 244 198 238 277 255 190 188

;

RUN;

 

DATA Cholesterol;

 

set cholestorol_input;

array level[10] C1-C10;

do i = 1 to 10;

do j = 1 to 3;

if j = 3 then treat = 'Placebo';

else if j = 2 then treat = 'B';

else if j = 1 then treat = 'A';

 

cholesterol_level = level[i];

output;

end;

end;

drop C1-C10 i;

RUN;

 

 

 

Desired Output - Abbreviated 

OBS treat   cholesterol_level

1    A            220

2    A            190

3    A            180

4    A            185

5    A            210

11   B            160

19   B            199

21   Placebo      240 

30   Placebo      188


Accepted Solutions
Solution
a week ago
Super User
Posts: 6,358

Re: Data input Array with nested do loop

Posted in reply to PaigeMiller

Ideally, when you add the IF/THEN statements that @PaigeMiller suggested, you'll place them after the SET statement.  There's no need to place them inside a DO loop where they execute 10 times instead of once.

View solution in original post


All Replies
Respected Advisor
Posts: 2,507

Re: Data input Array with nested do loop

You have 3 input rows and 10 numbers on each row, that's 30 data points. But then for each number, you run the loop indexed by J, which executes the OUTPUT statement 3 times, resulting in 90 data points.


Get rid of the J loop. Instead of

 

if j=3 then treat='Placebo';

use

if _n_=3 then treat='Placebo';
--
Paige Miller
Solution
a week ago
Super User
Posts: 6,358

Re: Data input Array with nested do loop

Posted in reply to PaigeMiller

Ideally, when you add the IF/THEN statements that @PaigeMiller suggested, you'll place them after the SET statement.  There's no need to place them inside a DO loop where they execute 10 times instead of once.

Contributor
Posts: 30

Re: Data input Array with nested do loop

Posted in reply to PaigeMiller

Thank you posting complete solution

 

DATA Cholesterol;

set cholestorol_input;

array level[10] C1-C10;

do i = 1 to 10;

if _n_ = 3 then treat = 'Placebo';

else if _n_ = 2 then treat = 'B';

else if _n_ = 1 then treat = 'A';

cholesterol_level = level[i];

output;

end;

drop C1-C10 i;

RUN;

Contributor
Posts: 30

Re: Data input Array with nested do loop

sorry guys copied the wrong solution 1st try

 

DATA Cholesterol;

set cholestorol_input;

     if _n_ = 3 then treat = 'Placebo';

else if _n_ = 2 then treat = 'B';

else if _n_ = 1 then treat = 'A';

array level[10] C1-C10;

do i = 1 to 10;

cholesterol_level = level[i];

output;

end;

drop C1-C10 i;

RUN;

 

Super User
Super User
Posts: 7,745

Re: Data input Array with nested do loop

[ Edited ]

Are you trying to read directly from your text lists? Then just use DO loops and @@ on the input statement.

data want;
  length treat $10 sample 8 chol 8 ;
  do treat='A','B','Placebo';
    do sample=1 to 10 ;
      input chol @@ ;
      output;
    end;
  end;
cards;
220 190 180 185 210 170 178 200 177 189
160 168 178 200 172 155 159 167 185 199
240 220 246 244 198 238 277 255 190 188
;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 97 views
  • 0 likes
  • 4 in conversation