Keep only the variables in a data set that end in a particular character

by Irina 23. May 2007 12:03
 

data

one;

input abab ret cca www ttt ggab;

datalines

;

1 2 3 4 5 6

;

options

mprint;

%macro vars(dsn,chr);

%local list dsid num len tmp rc;

%let list=;

%let dsid=%sysfunc(open(&dsn));

%let num=%sysfunc(attrn(&dsid,nvars));

%let len=%length(&chr);

%do i = 1%to #

%let var&i=%sysfunc(varname(&dsid,&i));

%let tmp=%sysfunc(reverse(&&var&i));

%if%sysfunc(reverse(%upcase(%substr(&tmp,1,&len)))) = %upcase(&chr) %then%do;

%let list=&list &&var&i;

%end;

%end;

%let rc=%sysfunc(close(&dsid));

%if

&list ne %then%do;

data final;

set &dsn(keep=&list);

run;

proc print data=final;

run;

%end

;

%else

%do;

data _null_;

file print;

put

"No variables found in dataset &dsn that end with &chr";

run;

%end

;

%mend

vars;

/* Invoke the macro passing in the name of the data set */

/* that contains all your variables. Also pass it the */

/* characters you would like the variables to end with. */

%vars(one,ab)

About the author

Irina Spivak Irina Spivak
Team Leader at G-Stat. More...


Send mail Email

Authors

Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Sign in

    eXTReMe Tracker