Examples use generated data (datalines) or SASHELP.
1 Code Block
DATA STEP Data
Explanation : This example demonstrates how to use the KEEP statement to specify which variables to keep in a new `employees_subset` data set. Only the specified variables (`name`, `address`, `city`, `state`, `zip`, `phone`) will be included in the final data set.
Copied!
data employees;
input name $ address $ city $ state $ zip $ phone $;
datalines;
John Doe 123 Main St Anytown CA 90210 555-1234
Jane Smith 456 Oak Ave Othercity NY 10001 555-5678
;
run;
data employees_subset;
set employees;
keep name address city state zip phone;
run;
1
DATA employees;
2
INPUT name $ address $ city $ state $ zip $ phone $;
3
DATALINES;
4
John Doe 123 Main St Anytown CA 90210555-1234
5
Jane Smith 456 Oak Ave Othercity NY 10001555-5678
6
;
7
RUN;
8
9
DATA employees_subset;
10
SET employees;
11
keep name address city state zip phone;
12
RUN;
2 Code Block
DATA STEP Data
Explanation : This example uses the KEEP statement to include only the `name` and `avg` variables in the `average` output data set. Variables `score1` through `score20`, from which `avg` is calculated, are not written to the `average` data set.
Copied!
data scores;
input name $ score1-score20;
datalines;
Alice 85 90 78 92 88 76 95 89 80 82 77 91 85 93 86 79 90 84 87 94
Bob 70 65 72 75 68 80 73 78 71 76 69 81 74 79 70 82 75 77 71 80
;
run;
data average;
set scores;
keep name avg;
avg=mean(of score1-score20);
run;
1
DATA scores;
2
INPUT name $ score1-score20;
3
DATALINES;
4
Alice 8590789288769589808277918593867990848794
5
Bob 7065727568807378717669817479708275777180
6
;
7
RUN;
8
9
DATA average;
10
SET scores;
11
keep name avg;
12
avg=mean(of score1-score20);
13
RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
« ever use KEEP and DROP in the same DATA step; it creates logical redundancy and can lead to confusion since DROP always takes precedence. Additionally, if you are reading from a massive dataset but only need a few columns, use the KEEP= option on the SET statement. This prevents unnecessary variables from ever entering the PDV, providing the maximum possible performance boost. »
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.