Contributor
Posts: 59

# How can I drop variables from Data Set A based on their membership of Data Set B

[ Edited ]

My SAS Code below is used to

a. Drop Variables from a Data Set

b. Construct Macro Variables containing Lists of Variable Names

How can I drop variables from Data Set A based on their membership of Data Set B

/* CONSTRUCT THE MACRO VARIABLE NUM_VARS_LIST TO CONTAIN THE LIST OF NUMERIC PREDICTOR VARIABLES FOR FUTURE ANALYSIS */

ODS OUTPUT NLEVELS = LIBXYZ.NUM_VAR_LEVELS;

PROC FREQ
DATA = LIBXYZ.DEVELOPMENT_SAMPLE_03N           /* SOURCE INPUT DATA SET */
NLEVELS ;
TABLES _NUMERIC_ / NOPRINT ;                              /* ONLY NUMERIC VARIABLES */
RUN;

/* IDENTIFY THE NON-VARIABLES TO BE DELETED FROM THE INPUT DATA SETS : SELECT THE NUMERIC VARIABLES TO BE DROPPED: */

DATA
LIBXYZ.DROPNUMVARS (RENAME = (TABLEVAR = NUMVAR_NAME));
SET LIBXYZ.NUM_VAR_LEVELS (WHERE = (NLEVELS = 1 OR NNONMISSLEVELS IN (0 , 1) ));
RUN;

/* There were 2 observations read from the data set LIBXYZ.NUM_VAR_LEVELS WHERE (NLEVELS=1) or NNONMISSLEVELS in (0, 1)  The data set LIBXYZ.DROPNUMVARS has 2 observations and 5 variables. */

TITLE ' NUMERIC VARIABLES WHERE [ NLEVELS = 1 OR NNONMISSLEVELS IN (0 , 1) ] TO BE DROPPED FROM THE DATA SET' ;

PROC PRINT
DATA = LIBXYZ.DROPNUMVARS;
RUN ;

/* DROP THE SELECTED NUMERIC VARIABLES */

How can I drop variables from Data Set LIBXYZ.DEVELOPMENT_SAMPLE_03N based on their membership of Data Set LIBXYZ.DROPNUMVARS

After that I want to create the the macro variable list:

PROC SQL
NOPRINT;
SELECT TABLEVAR
INTO :NUM_VARS_LIST SEPARATED BY ' '
FROM LIBXYZ.NUM_VAR_LEVELS                       /* ODS DATA SET NLEVELS = NUM_VAR_LEVELS */
;
QUIT;

/********************************************************************************************************************************/

PROC Star
Posts: 8,149

## Re: How can I drop variables from Data Set A based on their membership of Data Set B

Why don't you just change the proc sql code and add a final data step that uses the macro variable? e.g.,

PROC SQL
NOPRINT;
SELECT numvar_name
INTO :NUM_VARS_LIST SEPARATED BY ' '
FROM LIBXYZ.DROPNUMVARS /* ODS DATA SET NLEVELS = NUM_VAR_LEVELS */
;
QUIT;

data LIBXYZ.want;
set LIBXYZ.DEVELOPMENT_SAMPLE_03N (drop=&num_vars_list.);
run;

HTH,

Art, CEO, AnalystFinder.com

Contributor
Posts: 59

## Re: How can I drop variables from Data Set A based on their membership of Data Set B

I will explore this tomorrow and then get back to you.

Regards

attachments. -##
Valued Guide
Posts: 505

## Re: How can I drop variables from Data Set A based on their membership of Data Set B

``````Not sure I understood your problem but this may help.

data a;
set sashelp.class;
run;quit;

data b;
set sashelp.class(keep=name age);
run;quit;

data keep_only_variables_in_b;
set b(where=(1=2)) a open=defer;
run;quit;

Up to 40 obs from keep_only_variables_in_b total obs=19

Obs    NAME                 AGE

1    Alfred                14
2    Alice                 13
3    Barbara               13
4    Carol                 14
...
15    Philip                16
16    Robert                12
17    Ronald                15
18    Thomas                11
19    William               15

NOTE: There were 0 observations read from the data set WORK.B.
WHERE 0 /* an obviously FALSE WHERE clause */ ;
NOTE: For OPEN=DEFER processing, all variables processed should be
specified by the first data set listed in the SET statement.
NOTE: Variable SEX, found on WORK.A, is being ignored.
NOTE: Variable HEIGHT, found on WORK.A, is being ignored.
NOTE: Variable WEIGHT, found on WORK.A, is being ignored.
NOTE: There were 19 observations read from the data set WORK.A.
NOTE: The data set WORK.KEEP_ONLY_VARIABLES_IN_B has 19 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time           0.00 seconds``````
Discussion stats
• 3 replies
• 209 views
• 0 likes
• 3 in conversation