DATA Step, Macro, Functions and more

how to keep variables easier

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

how to keep variables easier

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.


Accepted Solutions
Solution
‎11-23-2013 02:12 AM
Trusted Advisor
Posts: 1,129

Re: how to keep variables easier

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


All Replies
Solution
‎11-23-2013 02:12 AM
Trusted Advisor
Posts: 1,129

Re: how to keep variables easier

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
SAS Super FREQ
Posts: 8,743

Re: how to keep variables easier

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=aSmiley Happy;

  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;

Frequent Contributor
Posts: 122

Re: how to keep variables easier

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.

Frequent Contributor
Posts: 122

Re: how to keep variables easier

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.

Super User
Posts: 17,836

Re: how to keep variables easier

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.

Frequent Contributor
Posts: 122

Re: how to keep variables easier

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 277 views
  • 3 likes
  • 4 in conversation