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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

5 REPLIES 5
ballardw
Super User

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;
PaigeMiller
Diamond | Level 26
proc sql noprint;
    select distinct var into :varnames separated by ' ' from listvar;
quit;

data bigdata;
    set bigdata(keep=&varnames);
run;
--
Paige Miller
Reeza
Super User

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;
art297
Opal | Level 21

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

 

 

hhchenfx
Rhodochrosite | Level 12

Thank you,

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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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