BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RickyS
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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
Astounding
PROC Star

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.

RickyS
Quartz | Level 8

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;

RickyS
Quartz | Level 8

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;

 

Tom
Super User Tom
Super User

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
;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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