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
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.
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';
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.
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;
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;
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
;
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!
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.
Ready to level-up your skills? Choose your own adventure.