BookmarkSubscribeRSS Feed
heatherml20
Fluorite | Level 6

*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;

 

4 REPLIES 4
LinusH
Tourmaline | Level 20

Depends on the source table.

Second example relies on a count column in the source, the first doesn't.

Data never sleeps
Cynthia_sas
Diamond | Level 26

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.

Cynthia_sas_1-1749137255622.jpeg

 

Notice that the lecture on the SUM statement explains that the SUM statement does an automatic RETAIN.

Cynthia_sas_0-1749136733426.jpeg

 

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:

Cynthia_sas_2-1749138032222.jpeg

 

Learning when to use the RETAIN statement and how the SUM statement works is one of the goals of this Lesson.

Cynthia

 

heatherml20
Fluorite | Level 6
Hi Cynthia,

My question was this: do I need the Count+1 in Example 1. You took it out so I assume we don't need it. I am referring to my Example 1:
data totalTraffic ;
set pg2.np_yearlyTraffic;
Count+1;
retain totTraffic 0;
totTraffic+Count;
format totTraffic comma12.;
keep ParkName Location Count totTraffic;
run;
Cynthia_sas
Diamond | Level 26
Hi:
Reread the instructions for the practice again and then run a PROC CONTENTS on the pg2.np_yearlyTraffic data table. In the instructions, there is a sentence that says "Notice that the Count column records the number of cars that have passed through a particular location." So, you need to use the COUNT column from the starting data to create totTraffic in this instance. In a different scenario, you might create your own variable, but it is not necessary in this case.
Cynthia

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 1546 views
  • 4 likes
  • 3 in conversation