BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SeanZ
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag

View solution in original post

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
Cynthia_sas
SAS Super FREQ

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;

SeanZ
Obsidian | Level 7

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.

SeanZ
Obsidian | Level 7

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.

Reeza
Super User

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.

SeanZ
Obsidian | Level 7

My bad. I should have looked it carefully. Yes. the problem is solved. Thanks.

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
  • 6 replies
  • 1106 views
  • 3 likes
  • 4 in conversation