*Hi, This is the lesson and there are two solutions below. In the first solution please let me know if you need .... count+1? both solutions are the same without it. If it is included, what is the reasoning. Thank you!! ********************************************************;
* LESSON 2, PRACTICE 1 *;
* a) Open the PG2.NP_YEARLYTRAFFIC table. Notice the *;
* Count column records the number of cars that have *;
* passed through a particular Location. *;
* b) Modify the DATA step to create a column, totTraffic,*;
* that is the running total of Count. *;
* b) Keep the ParkName, Location, Count, and *;
* totTraffic columns in the output table. *;
* c) Format totTraffic so values are displayed with *;
* commas. *;
***********************************************************;
data totalTraffic ;
set pg2.np_yearlyTraffic;
Count+1; *Running Total of Count */;
retain totTraffic 0;
totTraffic+Count;
format totTraffic comma12.;
keep ParkName Location Count totTraffic;
run;
/*OR*/
data totalTraffic;
set pg2.np_yearlytraffic;
totTraffic+Count;
keep ParkName Location Count totTraffic;
format totTraffic comma12.;
run;
Depends on the source table.
Second example relies on a count column in the source, the first doesn't.
Hi:
You might want to go back and revisit the lectures on the RETAIN statement and the SUM statement. In this code (I changed the output data table names with 1 and 2), You can see that the only difference is the RETAIN statement. The fact that Example 2 works the same as Example 1 tells you something about how the SUM statement works.
Notice that the lecture on the SUM statement explains that the SUM statement does an automatic RETAIN.
Thus, the RETAIN statement in the first example is not necessary for purposes of having the program work. On the other hand, using a RETAIN statement, even with a SUM statement makes it clearer that totTraffic is the variable being accumulated and retained.
Understanding when you need to use RETAIN and when it's not absolutely required is one of the objectives for this practice. However, you can break the SUM statement like this:
Learning when to use the RETAIN statement and how the SUM statement works is one of the goals of this Lesson.
Cynthia
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.