BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

Hi SAS instructors!

Now I am in  lesson 7, practice, level 2

Phil_NZ_1-1617756013302.png

In this practice, there is one thing I think redundant or maybe I fall into a fallacy.

This is the requirement and the answer

Phil_NZ_2-1617756109690.png

The dataset  is as below:

ParkName	CampType	CampCount
Acadia NP	Tent	    152,811
Acadia NP	RV	        46,629
Acadia NP	Backcountry	1,324
Amistad NRA	Tent	    38
Amistad NRA	RV	        8,265

I am wondering why you retain the ParkName here, because the table has been in narrow version so the ParkName will not be set as missing after each iteration.

data camping_wide (keep= parkname tent rv backcountry);
	set pg2.np_2016camping;
	by ParkName;
	retain Tent RV Backcountry;

	if camptype='Tent' then
		Tent=campcount;
	else if camptype='RV' then
		RV=campcount;
	else BACKCOUNTRY=campcount;
	format tent rv backcountry comma15.;

	if last.ParkName then
		output;
run;

I generate my code without retaining the ParkName and my result is similar to the one when applying your code

Phil_NZ_0-1617755989554.png

I also present the debugger here for iteration 4 for clarifying purpose.

If you retain the ParkName with any purpose that I did not notice, please let me know.

 

Warm regards,

Phil.

 

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Including PARKNAME in the RETAIN statement in the data step has no effect.  It does not harm, but it serves no purpose. Perhaps they did it just because they copied the KEEP statement?

 

If they wanted the RETAIN statement to actual "retain" the variable PARKNAME that is not needed because PARKNAME must already be retained because it is coming from the input dataset.  If has to be in the input dataset or else the BY statement will generate an error.  Also note that in this simple data step the fact that PARKNAME is retained has no real effect because as soon as the SET statement executes the value retained is replaced with the value read from the input dataset.

 

If the wanted the RETAIN statement as a "trick" to insure the order of the variables without having to fully define the variables type and length then the retain statement would need to be before the SET statement (at least for the variables like PARKNAME that are coming from the input dataset.)

View solution in original post

3 REPLIES 3
Reeza
Super User

What is the order of the variables when you print the original data set?

 

Phil_NZ
Barite | Level 11

Hi @Reeza 

The original dataset is documented above, I quoted again here, sorry for a mess in the post.

Phil_NZ_0-1617770498901.png

Warmest regards.

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Tom
Super User Tom
Super User

Including PARKNAME in the RETAIN statement in the data step has no effect.  It does not harm, but it serves no purpose. Perhaps they did it just because they copied the KEEP statement?

 

If they wanted the RETAIN statement to actual "retain" the variable PARKNAME that is not needed because PARKNAME must already be retained because it is coming from the input dataset.  If has to be in the input dataset or else the BY statement will generate an error.  Also note that in this simple data step the fact that PARKNAME is retained has no real effect because as soon as the SET statement executes the value retained is replaced with the value read from the input dataset.

 

If the wanted the RETAIN statement as a "trick" to insure the order of the variables without having to fully define the variables type and length then the retain statement would need to be before the SET statement (at least for the variables like PARKNAME that are coming from the input dataset.)

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

LIBNAME 101

Follow along as SAS technical trainer Dominique Weatherspoon expertly answers all your questions about SAS Libraries.

Find more tutorials on the SAS Users YouTube channel.

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