a. Create a DATA set called WOMAN. (It’s up to you whether to make this data set temporary or permanent.
b. Create the outer DO loop for the variable AGE. Since the woman is of voting age and under 100, AGE will need to go from 18 to 99.
c. Create a nested DO loop (inside the outer loop for PETS). Since we know that the woman has both cats and dogs, she must have at least two of each. Thus pets must be at least 4. Since her age is at least 18, we know that PETS×ADDRESS can’t be more than 57,165/18=3,175.83333. Since address can’t be less than 1, PETS can’t be more than 3,175. So the PETS loop should go from 4 to 3,175.
d. Create another nested DO loop (inside the PETS loop, for ADDRESS. Since AGE can’t be less than 18 and PETS can’t be less than 4, ADDRESS can’t be more than 57,165/(18×4)=793.958333. So the ADDRESS loop should go from 1 to 793.
e. Place an output statement in the middle of the three loops so that SAS creates an observation for each combination.
f. In particular, the log window will report the number of observations in your data set. If you did this correctly, there should be (99-17)=82 different values for AGE, (3175-3)=3,172 different values for PETS, and 793 different values for ADDRESS. This means there should be 82×3,172×793=206,262372 observations in your data set.
Hi guys I need help when I create this loop I don't get the number of supposed observations;" 206,262372" this is what I do;
LIBNAME ECON581'\\TSCLIENT\G\ECON581';
DATA WOMAN;
DO AGE=18 TO 99;
DO PETS=4 TO 3175;
DO ADDRESS=1 TO 793;
OUTPUT;
END;
OUTPUT;
END;
OUTPUT;
END;
RUN;
But I get 206,522,658. Any idea what I might be doing wrong
Hi @oamune,
Of course, experienced SAS programmers spot this error immediately and can tell you how to correct it. But I think you would have had a more sustainable learning experience if you had scrutinized your code and the resulting dataset yourself.
Essentially, this DATA step uses only two different statements (in addition to the DATA statement): the iterative DO and the OUTPUT statement. So, it's not a big deal to look these up in the documentation (see the links) and then walk through each line of the code and verbalize what the statements are doing. To facilitate checking the output dataset, it's advisable to create an example with much smaller ending values of the DO loops so that dataset WOMAN will have, say, <50 observations. Then you could display the entire dataset and see the unwanted extra observations (with some PETS and all ADDRESS values outside the intended ranges!) produced by the surplus OUTPUT statements.
Note that the ability to investigate unexpected results (such as numbers of observations in a dataset) is a very important skill which is often needed in practice, typically with more complicated code and data structures.
Have fun learning SAS and enjoy the SAS Support Communities!
You only need the output in the innermost loop.
wow! Thanks for the swift response, I will try this and give you a feedback if it works. Thanks!
Make your code look good:
data woman;
do age = 18 to 99;
do pets = 4 to 3175;
do address = 1 to 793;
output;
end;
end;
end;
run;
As other has said, remove the two extra output statements. But, my main issue with this is the instruction, 82×3,172×793 = 206,262,472 not as the assignment says 206,262,372. 🙂
DATA WOMAN;
DO AGE=18 TO 99;
DO PETS=4 TO 3175;
DO ADDRESS=1 TO 793;
OUTPUT;
END;
*OUTPUT;
END;
*OUTPUT;
END;
RUN;
Hi @oamune,
Of course, experienced SAS programmers spot this error immediately and can tell you how to correct it. But I think you would have had a more sustainable learning experience if you had scrutinized your code and the resulting dataset yourself.
Essentially, this DATA step uses only two different statements (in addition to the DATA statement): the iterative DO and the OUTPUT statement. So, it's not a big deal to look these up in the documentation (see the links) and then walk through each line of the code and verbalize what the statements are doing. To facilitate checking the output dataset, it's advisable to create an example with much smaller ending values of the DO loops so that dataset WOMAN will have, say, <50 observations. Then you could display the entire dataset and see the unwanted extra observations (with some PETS and all ADDRESS values outside the intended ranges!) produced by the surplus OUTPUT statements.
Note that the ability to investigate unexpected results (such as numbers of observations in a dataset) is a very important skill which is often needed in practice, typically with more complicated code and data structures.
Have fun learning SAS and enjoy the SAS Support Communities!
got it Thanks
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: