Hi SAS instructors!
Now I am in lesson 7, practice, level 2
In this practice, there is one thing I think redundant or maybe I fall into a fallacy.
This is the requirement and the answer
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
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.
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.)
What is the order of the variables when you print the original data set?
Hi @Reeza
The original dataset is documented above, I quoted again here, sorry for a mess in the post.
Warmest regards.
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.)
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.
Find more tutorials on the SAS Users YouTube channel.