Hi, I have a dataset with hundreds of variables. I only want to keep some of them. Is there any easy way to pick them? Or I have to write their names separately. One example would be, I want to keep a1b1_x, a1b2_x,....a1b10_x, a2b1_x,.... These are 10X10 variables with _x at the end. How can I write it in the keep option? Thanks.
Please use contents procedure to get all the variable names and then create a macro variable with variable names that end with _x and in the last step, in the keep option call that macro variable. this will put only those variables that end with _x i the dataset.
proc contents data=have out=temp(keep=name);
run;
proc sql;
select trim(left(name)) into: list separated by ' ' from temp where name like '%_x';
quit;
%put &list;
data want;
set have(keep=&list);
run;
Thanks,
Jagadish
Please use contents procedure to get all the variable names and then create a macro variable with variable names that end with _x and in the last step, in the keep option call that macro variable. this will put only those variables that end with _x i the dataset.
proc contents data=have out=temp(keep=name);
run;
proc sql;
select trim(left(name)) into: list separated by ' ' from temp where name like '%_x';
quit;
%put &list;
data want;
set have(keep=&list);
run;
Thanks,
Jagadish
Hi: If ALL of the variables start with 'a' and no other variables start with a, then there is a simpler method than what is shown using PROC CONTENTS (although that method will work, it does perform some unnecessary processing in this case.) See the example below and compare the output for the NEW1 data vs the NEW2 data (as shown in the PROC CONTENTS). Using the colon modifier allows you to select a list of variables very easily.
Cynthia
ods _all_ close;
title; footnote;
data new1;
a1b1_x = 9;
a1b2_x = 8;
a1b3_x = 7;
a1b4_x = 6;
a2b1_x = 5;
a2b2_x = 4;
a2b3_x = 3;
a2b4_x = 2;
xxx = 10;
yyy = 20;
zzz = 30;
run;
ods html file='c:\temp\new1.html' style=sasweb;
proc contents data=new1;
title 'Default Behavior Keeps ALL Vars';
run;
ods _all_ close;
data new2(keep=a:);
a1b1_x = 9;
a1b2_x = 8;
a1b3_x = 7;
a1b4_x = 6;
a2b1_x = 5;
a2b2_x = 4;
a2b3_x = 3;
a2b4_x = 2;
xxx = 10;
yyy = 20;
zzz = 30;
run;
ods html file='c:\temp\new2.html' style=sasweb;
proc contents data=new2;
title 'Using Name Prefix List';
run;
ods _all_ close;
thank you for your reply. I ran your code and get following error
199 ods html file='c:\temp\new1.html' style=sasweb;
NOTE: Writing HTML Body file: c:\temp\new1.html
ERROR: Physical file does not exist, c:\temp\new1.html.
ERROR: No body file. HTML output will not be created.
WARNING: No output destinations active.
Thanks.
Hi Jagadishkatam, thank you for your answer. it seems that the variables I want to keep are not listed in one variable "name". There are bench of variables in my dataset and I only want to keep those variable with _x as the end. Your code to me seems to be one variable "name" including all variables' names. Do I have to first create one variable "name" to include all variable names? If so, how to create this variable? Thanks.
The solution above (jag's) uses proc contents to list all the variables in the dataset called temp. Change the data= to reflect your dataset.
proc contents data=have out=temp(keep=name);
run;
If you open the dataset called temp you should see 1 variable, called name, that has all of the variables in your dataset.
Then you need to create a MACRO variable (called list here) to store all the variables that end with _x. If you see _X you may want to make it case insensitive.
proc sql;
select trim(left(name)) into: list separated by ' ' from temp where upcase(name) like '%_X';
quit;
Then you use that macro variable in your keep statement:
%put &list;
data want;
set have(keep=&list);
run;
If this doesn't work, post the exact code you used and your log.
My bad. I should have looked it carefully. Yes. the problem is solved. Thanks.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.