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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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