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)

Related posts

Comments

3/10/2008 4:01:50 AM

FYI, sorry, it's me again. There is also a method using a simple SQL procedure!
;o)
(check all the system view in the sashelp library, you will find usefull informations, vtables, vcolumn, etc...)
cheers!


proc sql noprint;
select name into:n separated by ' '
from sashelp.vcolumn
where libname=upcase("sashelp") and memname=upcase("prdsale") and name like '%ON';
quit;
%put &n.;

again_the_french_SAS_user

10/8/2011 4:17:29 AM

Thank you for sharing, I really enjoyed your posts and pictures they were great! I think that all of us who follow this blog have that same feeling that you got from visiting.

beijing youth hostels

Add comment


(will show your Gravatar icon)  





Live preview

2/5/2012 9:27:59 PM

 

About the author

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


Send mail Email

Blogroll

    Disclaimer

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

    © Copyright 2012

    Sign in

    eXTReMe Tracker