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

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 )

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

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 )

kashili
Calcite | Level 5

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

Linlin
Lapis Lazuli | Level 10

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;

Tom
Super User Tom
Super User

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;

UrvishShah
Fluorite | Level 6

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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