DATA Step, Macro, Functions and more

Keep a list of variables

Accepted Solution Solved
Reply
Super Contributor
Posts: 506
Accepted Solution

Keep a list of variables

Hi Everyone,

I have 2 files. One is a big datafile, and one is the list of variable name and want to keep.

basically I want something like that

 

data want; set bigfile;

keep a b c; run;

 

But the list is long and keep in the second file.

 

Can you help me with that?

 

Thank you,

 

HHCFX

 

I try to transpose and create a temp code like "KEEP a b c ;"

then do

data want ; set bigdata;
%include tmp;
;run;

 

but dont know how to create that  "KEEP a b c ;"

 


data bigdata; 
input a b c d e f;
datalines;
1 2 3 4 5 6
;run;


data listvar; 
input var $;
datalines;
a
b
c
;run;

Accepted Solutions
Solution
‎03-29-2018 11:46 AM
Super User
Posts: 13,913

Re: Keep a list of variables

One way:

data bigdata; 
input a b c d e f;
datalines;
1 2 3 4 5 6
;run;

data listvar; 
input var $;
datalines;
a
b
c
;
run;
proc sql noprint;
    select var into: varlist separated by ' '
    from listvar;
quit;

data smalldata;
   set bigdata (keep=&varlist.);
run;

View solution in original post


All Replies
Solution
‎03-29-2018 11:46 AM
Super User
Posts: 13,913

Re: Keep a list of variables

One way:

data bigdata; 
input a b c d e f;
datalines;
1 2 3 4 5 6
;run;

data listvar; 
input var $;
datalines;
a
b
c
;
run;
proc sql noprint;
    select var into: varlist separated by ' '
    from listvar;
quit;

data smalldata;
   set bigdata (keep=&varlist.);
run;
Respected Advisor
Posts: 3,258

Re: Keep a list of variables

proc sql noprint;
    select distinct var into :varnames separated by ' ' from listvar;
quit;

data bigdata;
    set bigdata(keep=&varnames);
run;
--
Paige Miller
Super User
Posts: 23,980

Re: Keep a list of variables

1. Create a macro variable list from listvar table - see PROC SQL create macro variables in the documentation.

 

See example 3 in the documentation here:

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n1y2jszlvs4hugn14nooftfrxhp3.htm&docset...

 

2. Use that macro variable in a Keep statement.

 

keep &keep_list;
Super User
Posts: 8,214

Re: Keep a list of variables

You already have a solution, but I noticed that no one showed you how to accomplish the task the way you originally wanted to do it. Here is one way:

data keepers;
  input keepers $;
  datalines;
Name
age
;

data _null_;
  file '/folders/myfolders/keepers.sas';
  set keepers end=lastone;
  if _n_ eq 1 then put 'keep';
  put keepers;
  if lastone then put ';';
run;

data want;
  set sashelp.class;
  %inc '/folders/myfolders/keepers.sas';
run;

Art, CEO, AnalystFinder.com

 

 

Super Contributor
Posts: 506

Re: Keep a list of variables

Thank you,

I thought I will you some sort of transpose, array, then catt() to add these var together.

 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 165 views
  • 3 likes
  • 5 in conversation