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

In a data step, I have came across different places to put for instance your keep statement. Example , with Pseudo Code: 

data MyTry(keep=ColA) ; *Alternative 1;
   set SomeData(keep = ColA);  *Alternative 2;
   keep ColA; *Alternative 3;
run; 

Putting the keep as in Alternative 2, often gives more efficient program due to reading in less data. 

But what is the difference between Alternative 1 and Alternative 3? Pros and cons with the alternatives? 
In my experience Alternative 1 is very rear (I have not seen it often). 

Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

#1 and #3 are very close, as they determine what is output, without affecting the presence of variables within the step.

#1 allows to be selective (if you have more than one dataset in the DATA statement, you can control the variables individually).

#2 filters what goes into the data step, so the other variables contained in the input dataset will not be present during the step.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

#1 and #3 are very close, as they determine what is output, without affecting the presence of variables within the step.

#1 allows to be selective (if you have more than one dataset in the DATA statement, you can control the variables individually).

#2 filters what goes into the data step, so the other variables contained in the input dataset will not be present during the step.

Astounding
PROC Star

For practical purposes, you know all you really need to know.

 

Complications arise if you use both tools.  For example, would these two programs get the same results or different results?

 

data want (drop=name);
   set have;
   keep name;
run;

data want (keep=name);
   set have;
   drop name;
run;

The complications get compounded if you use RENAME in one spot, but KEEP or DROP in the other spot.  Should KEEP (or DROP) refer to the original name or to the new name?

 

There are rules about that, but rather than memorize them just avoid the situation.

Tom
Super User Tom
Super User

The one rule I remember is that dataset options are processed in alphabetical order.

 

DROP/KEEP

RENAME

WHERE

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 590 views
  • 7 likes
  • 4 in conversation