DATA Step, Macro, Functions and more

want to write to global dataset from a macro(but it is erasing previous writes)

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

want to write to global dataset from a macro(but it is erasing previous writes)

I have a macro which processes a dataset. I want to preserve the final outcome of this processing. So I am writing the final outcome to a global dataset(I could not think of any other alternative in SAS to make macro preserve the final outcome). This works good if I call macro once. However when I call macro more than once, only the last write is preserved(apparently previous writes are erased and I am preserved with only 1 final outcome in global dataset).

Assuming writing to global dataset is preferred way to preserve the final outcome of a macro, how can I RETAIN the writes to global dataset (instead of overwriting the previous content and preserving only 1 write in global dataset). Here is SAS snippet I am using..thank you

%macro LMGenerate(mymktdata,yeartag);

data &mymktdata.highliquidity;

*blah blah;

run;

data &mymktdata.lowliquidity;

*blah blah;

run;

*this is basically difference of above 2 datasets

data  liquiditypremiums;
  update &mymktdata.lowliquidity(in = a)  &mymktdata.highliquidity(in =b);
  by year;

  if a & b then
  do;
   netpremium=lowliquiditytotal-highliquiditytotal;
   output;
  end;

run;

%mend  LMGenerate;

*global data;

data liquiditypremiums;
run;

%LMGenerate(yr2010mktdata,2010 )
%LMGenerate(yr2011mktdata,2011 )


Accepted Solutions
Solution
‎02-19-2013 10:53 PM
Super User
Super User
Posts: 6,498

Re: want to write to global dataset from a macro(but it is erasing previous writes)

Not sure what you mean by a "global" dataset.  You only seem to be creating WORK datasets.

If you want to append results to a dataset you might want to look at PROC APPEND.

Here is a trivial example.

%macro xx(value);

* Calculate new values ;

data new;

  x=&value;

run;

proc append base=global data=new force;

run;

%mend xx;

%xx(1);

%xx(2);

proc print data=global;

run;

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: want to write to global dataset from a macro(but it is erasing previous writes)

Hi ,

try adding another macro variable:

%macro LMGenerate(mymktdata,yeartag,dsn);

data &mymktdata.highliquidity;

*blah blah;

run;

data &mymktdata.lowliquidity;

*blah blah;

run;

*this is basically difference of above 2 datasets

data &dsn._ liquiditypremiums;
  update &mymktdata.lowliquidity(in = a)  &mymktdata.highliquidity(in =b);
  by year;

  if a & b then
  do;
   netpremium=lowliquiditytotal-highliquiditytotal;
   output;
  end;

run;

%mend  LMGenerate;

 

%LMGenerate(yr2010mktdata,2010,aaa )
%LMGenerate(yr2011mktdata,2011,bbb )

Occasional Contributor
Posts: 13

Re: want to write to global dataset from a macro(but it is erasing previous writes)

Hello Linlin

what is 'aaa' and 'bbb'? Not sure how to fit in macro variable here (I am newbie..).

I have a  dataset outside macro and I need my macro to append some values to this  dataset (so that I can do post processing after all macros are executed).

thank you

Super Contributor
Posts: 1,636

Re: want to write to global dataset from a macro(but it is erasing previous writes)

Hi,

try the code below to see if you get what you want (repace the RED part with your real dataset name, the dataset you want to add obsevations on):

%macro LMGenerate(mymktdata,yeartag);
data &mymktdata.highliquidity;
*blah blah;
run;

data &mymktdata.lowliquidity;
*blah blah;
run;

*this is basically difference of above 2 datasets
data  _&mymktdata;
  update &mymktdata.lowliquidity(in = a)  &mymktdata.highliquidity(in =b);
  by year;
  if a & b then
  do;
   netpremium=lowliquiditytotal-highliquiditytotal;
   output;
  end;
run;

%mend  LMGenerate;


%LMGenerate(yr2010mktdata,2010 )
%LMGenerate(yr2011mktdata,2011 )
*global data;
data want;
  set your_global_dataset_name _:;
run;

Solution
‎02-19-2013 10:53 PM
Super User
Super User
Posts: 6,498

Re: want to write to global dataset from a macro(but it is erasing previous writes)

Not sure what you mean by a "global" dataset.  You only seem to be creating WORK datasets.

If you want to append results to a dataset you might want to look at PROC APPEND.

Here is a trivial example.

%macro xx(value);

* Calculate new values ;

data new;

  x=&value;

run;

proc append base=global data=new force;

run;

%mend xx;

%xx(1);

%xx(2);

proc print data=global;

run;

Regular Contributor
Posts: 195

Re: want to write to global dataset from a macro(but it is erasing previous writes)

Hello,

By default, SAS will replace the dataset if you supply the same name in your DATA statement that you named in your UPDATE, SET or MERGE statement...So here when you call the macro more than once, the you lose the prior update and finally you only have one dataset which overwrite the prior datasets...

I think in this case, use GENMAX  = n option in data step...

%macro LMGenerate(mymktdata,yeartag);

data &mymktdata.highliquidity;

*blah blah;

run;

data &mymktdata.lowliquidity;

*blah blah;

run;

*this is basically difference of above 2 datasets

data  liquiditypremiums (genmax = 3);
  update &mymktdata.lowliquidity(in = a)  &mymktdata.highliquidity(in =b);
  by year;

  if a & b then
  do;
   netpremium=lowliquiditytotal-highliquiditytotal;
   output;
  end;

run;

%mend  LMGenerate;

*global data;

data liquiditypremiums;
run;

%LMGenerate(yr2010mktdata,2010 )
%LMGenerate(yr2011mktdata,2011 )

Thanks,

Urvish

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 208 views
  • 6 likes
  • 4 in conversation