DATA Step, Macro, Functions and more

Using date macro to segment into separate datasets

Reply
Frequent Contributor
Posts: 141

Using date macro to segment into separate datasets

I produced the dataset b_monthly with date1, which is a date varaible

LN_NO

TPR_DATA_AS_OF_DT

Month

YEARMONTH_KEY

date1

0000000000

31Dec2014

201412

201412

201412

0000000000

29Nov2014

201411

201411

201411

0000000000

31Oct2014

201410

201410

201410

0000000000

30Sep2014

201409

201409

201409

0000000000

30Aug2014

201408

201408

201408

0000000000

31Jul2014

201407

201407

201407

Date1 is a date variable.  I tried the following to segment it into separate datasets

%Macro dmart (date1);

data d_Monthly_&date1.;

set b_monthly;

run;

%Mend;

I ran the code but nothing was produced.  I want to produce separate datasets such as

d_monthly_201412

d_monthly_201411

Valued Guide
Posts: 860

Re: Using date macro to segment into separate datasets

Here's a start:

data dateat;

      infile cards;

      informat ln_no $10. TPR_DATA_AS_OF_DT date9. Month YEARMONTH_KEY yymmn6.;

      format ln_no $10. TPR_DATA_AS_OF_DT date9. Month YEARMONTH_KEY yymmn6.;

      input LN_NO$ TPR_DATA_AS_OF_DT Month YEARMONTH_KEY date1;

     cards;

0000000000 31Dec2014 201412 201412 201412

0000000000 29Nov2014 201411 201411 201411

0000000000 31Oct2014 201410 201410 201410

0000000000 30Sep2014 201409 201409 201409

0000000000 30Aug2014 201408 201408 201408

0000000000 31Jul2014 201407 201407 201407

;

run;

%macro date1;

%do i = 201412 %to 201407 %by -1;

data d_monthly_&i;

set dateat;

where date1 = &i;

run;

%end;

%mend;

%date1;

Super Contributor
Posts: 275

Re: Using date macro to segment into separate datasets

%Macro dmart (date1);

data d_Monthly_&date1.;

set b_monthly;

if &date1=date1 then output;

run;

%Mend;

SAS Super FREQ
Posts: 708

Re: Using date macro to segment into separate datasets

Hi

Have a look at this blog post http://blogs.sas.com/content/sasdummy/2012/03/20/sas-program-by-processing/ by Chris Hemedinger

It shows very nicely on how to approach this. It will show how to find unique values for a given variable and then have a loop for each value and run some code. Since you already have char variables that are the value for spliting up the data sets I would use either Month or YEARMONTH_KEY

Bruno

Super User
Super User
Posts: 7,942

Re: Using date macro to segment into separate datasets

Posted in reply to Bruno_SAS

Seems a fair bit of code to do (assuming the date field is distinct per the example, otherwise further below) what with all those macro variables, and index through it.  Just use standard datastep with a call execute?:

data _null_;

     set have;

     call execute(cats('data want_',date,'; set have; run;'));

run;

And if its not the distinct list, just add:

proc sql;

     create table LOOP as

     select     distinct DATE

     from       HAVE;

quit;

data _null_;

     set loop;

     call execute...;

run;

Contributor
Posts: 65

Re: Using date macro to segment into separate datasets

If the start and end dates are not known then you can try this

proc sql;

create table dates as select distint date1 as dates from b_monthly;

quit;

data _null_;

set dates;

call execute('data d_monthly_'||strip(dates)||';set b_monthly(where date1 = '||strip(dates)||');run');

run;

Regular Contributor
Posts: 227

Re: Using date macro to segment into separate datasets

this is a FAQ

Here is a category with several answers.

http://www.sascommunity.org/wiki/Category:Making_subsets

short answers:

* do not split a data set

* Why? How about by processing?

Ask a Question
Discussion stats
  • 6 replies
  • 319 views
  • 1 like
  • 7 in conversation