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

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
Barite | Level 11

Thank you,

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

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 4049 views
  • 3 likes
  • 5 in conversation