BookmarkSubscribeRSS Feed
JonDickens1607
Obsidian | Level 7

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;

 

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

 

Thanks for your help.

3 REPLIES 3
art297
Opal | Level 21

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

 

 

JonDickens1607
Obsidian | Level 7
Thanks for your suggestion.

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

Regards

##- Please type your reply above this line. Simple formatting, no
attachments. -##
rogerjdeangelis
Barite | Level 11
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 894 views
  • 0 likes
  • 3 in conversation