DATA Step, Macro, Functions and more

concatenate datasets which exists in the library

Reply
Occasional Contributor
Posts: 8

concatenate datasets which exists in the library

Hi,

I have a library "have" which contains dataset of each day. e.g data20150101,data20150102,data20150105...

and some irrelevant dataset e.g irrelevant1, irrelevant2, irrelevant3..

Sat and Sun dataset are not available. i.e no data20150103, data20150104


I want to create a new dataset "want" combining all the daily files for a month but not those irrelevant dataset,

any possible way to do it automatically? I am now doing it manually with set statement..


Thanks so much for the help!

Super User
Posts: 5,424

Re: concatenate datasets which exists in the library

You could probably use a combination of macro logic, name pattern matching and a control table (s).

Data never sleeps
Super User
Posts: 11,343

Re: concatenate datasets which exists in the library

If the irrelevant data sets do not begin with DATA201501 then:

Data want;

     set data201501: ;

run;

would work to combine all of the, I assume January data sets.

If you are placing the resulting data set in the same library then you may not want to name it data201501<anything> as this code will then append this dataset as well.

Super User
Posts: 19,770

Re: concatenate datasets which exists in the library

Adding on to solution, you can use the indsname option if you want to identify the source data set.

Data want;

     set data201501: indsname=source ;

     source=source;

run;

Super User
Posts: 3,250

Re: concatenate datasets which exists in the library

I'd go with a macro-based solution that contains a loop with a supplied starting date and end date. I would also use the EXIST function to be able to report on any days where there is no dataset and to add to the SET statement only datasets that exist.

Super User
Posts: 10,018

Re: concatenate datasets which exists in the library

Use dictionary table to pick up these wanted tables.

data data20150101 data20150102 data20150103 data20150104 data20150105 irrelevant1 irrelevant2 irrelevant3;
   infile cards  dsd truncover; 
   input id : $20. i : $20. @;
   id=compress(id,,'kd');
   retain v 1;
   do until(missing(i));
    output;
     input i : $20. @;
   end;
   cards; 
ID1,
ID2,Trait1,Trait2,Trait3
ID3,Trait2,Trait3,Trait5
ID4,Trait5
ID5,Trait1
;;;;
   run; 

proc sql;
 select memname into : list separated by ' '
  from dictionary.members
   where libname='WORK' and memtype='DATA' and
    weekday(input(compress(memname,,'kd'), anydtdte.)) in (2:6);
quit;
data want;
 length tname $ 40;
 set &list indsname=indsname;
 tname=indsname;
run;

Xia Keshan

Ask a Question
Discussion stats
  • 5 replies
  • 306 views
  • 1 like
  • 6 in conversation