In the previous post, we saw if else condition. In this post, we can see how can we improve programming using do loop. Some times we come up with a situation where we should use two or more IF statements for the same conditions. But we can avoid this by using DO and END statements where we can test a condition at first and then perform several actions. We will see this by the below examples.
# Problem:
There are three speed-reading methods(A, B and C) and we are assigning 10 subjects to each of the three methods. The results are given below in three lines of reading speeds, each line represents the results from each of the three methods.
250 255 256 300 244 268 301 322 256 333
267 275 256 320 250 340 345 290 280 300
350 350 340 290 377 401 380 310 299 399
Now, we want each observation should contain Method A,B or C and score. There should be 30 observation in the data set.
# Solution:
data A15028.A28_method; *using Do loop assigning three methods and then the 10 subjects; do method = 'method A','method B', 'method C'; do i = 1 to 10; * @ symbol tells SAS more data to be added for the method; input score @; ouput; end; end; drop i; datalines; 250 255 256 300 244 268 301 322 256 333 267 275 256 320 250 340 345 290 280 300 350 350 340 290 377 401 380 310 299 399 ; proc print data=A15028.A28_method; run;
#Output:
With @ symbol
Output without @ Symbol
#Learning:
So now we know that when and how to use Do loops and why it is efficient than IF statement. Also we learned about the nested DO loop. Moreover we now know how to read more data when it is present in the single line (by @ symbol).
No comments:
Post a Comment