I' am trying to read in my data using an infile statement and then use a do loop to replicate the data several times based on the value of two variables, replicates and Samp. When I add the do loop in, I get this error : v
Invalid DO loop control information, either the INITIAL or TO expression is missing or the BY
expression is missing, zero, or invalid.
Does anyone have any idea why this is happening.
For reference here is what the data looks like before:
ID Replicate Samp
1329 5 2
and here is what I want it to look like afterwards:
ID reps. Samp rep1. samp1
1329 5 2 1 1
1329 5 2 1 2
1329 5 2 2 1
1329 5 2 2 2
1329 5 2 3 1
1329 5 2 3 2
1329 5 2 4 1
1329 5 2 4 2
1329 5 2 5 1
1329 5 2 5 2
Here is my current code to do this
The data is formatted a little weird so the infile and do while loops are necessary to properly read in the data
data carsim;
infile 'S:\sasdata\cars.txt' firstobs=2 dlm='09'x truncover ;
input dist : $6. reps : samp @ ;
/*Remove any observations where size is missing*/
do while(not missing(Samp));
output;
input Samp @;
end;
/* rep1 = observation number within the sample*/
/* samp1 = Sample Number
/*Replicate dataset according to rep and size variables*/
do rep1 = 1 to reps by 1;
do samp1 = 1 to size by 1;
output;
end;
end;
run;
SIZE is not set anywhere in your code. You will see a NOTE about that in the log ("uninitialized").
Please show us the LOG (all of it for your data step, every single line of code plus NOTEs, WARNINGs and ERRORs of this data step, with nothing chopped out). Please do NOT show us errors disconnected from the code.
I think we would also need to see a portion of the input data from the .txt file, presented following these instructions and not via any other method.
Do you really need to first DO loop? That is do some of the lines of text have multiple SAMP values?
If not then remove it.
If yes then move the second DO loop inside the first.
data carsim;
infile 'S:\sasdata\cars.txt' firstobs=2 dlm='09'x truncover ;
input dist : $6. reps : samp @ ;
do while(not missing(Samp));
/* rep1 = observation number within the sample*/
/* samp1 = Sample Number
/*Replicate dataset according to rep and size variables*/
do rep1 = 1 to reps by 1;
do samp1 = 1 to SAMP by 1;
output;
end;
end;
input Samp @;
end;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.