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;
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;
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;
proc sql noprint;
select distinct var into :varnames separated by ' ' from listvar;
quit;
data bigdata;
set bigdata(keep=&varnames);
run;
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:
2. Use that macro variable in a Keep statement.
keep &keep_list;
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
Thank you,
I thought I will you some sort of transpose, array, then catt() to add these var together.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.