DATA Step, Macro, Functions and more

Create a table to append everymonth data

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Create a table to append everymonth data

Hi, let us say my january output is:

MonthPolicyPremium
Jan11220
Jan11323
Jan11424

 

So in next month, i mean in February I will have output:

MonthPolicyPremium
feb20045
feb20147
feb20250

 

Now I want to creat a table which will append both month data, :

MonthPolicyPremium
jan11220
jan11323
jan11424
feb20045
feb20147
feb20250

 But suppose in feb month i run my program 2 times, So it should not append the data twice, I mean it should not generate table like this:

MonthPolicyPremium
jan11220
jan11323
jan11424
feb20045
feb20147
feb20250
feb20045
feb20147
feb20250

 

SO my point is to append the data only once, after that it should not append for that particular month.


Accepted Solutions
Solution
‎06-28-2016 11:52 AM
PROC Star
Posts: 1,561

Re: Create a table to append everymonth data

[ Edited ]

proc append is the fastest by far. Any form of update will be much slower.

 

To ensure proc append is only called when new data is added, something like this should work:

 

data ALL;
  MTH='01jan2010'd;
run;

data T2;
  MTH='01feb2010'd;
run;

%macro append_if_new(base=, data=);
%local nobs;
data _null_; 
  if 0 then set ALL nobs=NOBS;
  call symputx('NOBS', NOBS);
data _null_; 
  set ALL(firstobs=&nobs. rename=(MTH=EXISTING_MONTH));
  set T2 (obs=1           rename=(MTH=NEW_MONTH));
  if NEW_MONTH ne EXISTING_MONTH then do;
    putlog "Appending data=&data to base=&base.. ";
    call execute("proc append base=&base data=&data;run;");
  end;
  else putlog "Data=&data already in base=&base..";
run;   
%mend;

%append_if_new(base=ALL, data=T2);
%append_if_new(base=ALL, data=T2);

 

View solution in original post


All Replies
Super User
Posts: 17,828

Re: Create a table to append everymonth data

Look at the update statement. 

 

If a record is different it updates it. If a record is missing it adds it. 

However you need key variables and they should be unique. Otherwise you have to do a manual check before you append the results  

Super User
Posts: 9,681

Re: Create a table to append everymonth data

add one more sql at the end of your code:

 

proc sql;

create table want as

 select distinct *

  from append_table ;

quit;

Solution
‎06-28-2016 11:52 AM
PROC Star
Posts: 1,561

Re: Create a table to append everymonth data

[ Edited ]

proc append is the fastest by far. Any form of update will be much slower.

 

To ensure proc append is only called when new data is added, something like this should work:

 

data ALL;
  MTH='01jan2010'd;
run;

data T2;
  MTH='01feb2010'd;
run;

%macro append_if_new(base=, data=);
%local nobs;
data _null_; 
  if 0 then set ALL nobs=NOBS;
  call symputx('NOBS', NOBS);
data _null_; 
  set ALL(firstobs=&nobs. rename=(MTH=EXISTING_MONTH));
  set T2 (obs=1           rename=(MTH=NEW_MONTH));
  if NEW_MONTH ne EXISTING_MONTH then do;
    putlog "Appending data=&data to base=&base.. ";
    call execute("proc append base=&base data=&data;run;");
  end;
  else putlog "Data=&data already in base=&base..";
run;   
%mend;

%append_if_new(base=ALL, data=T2);
%append_if_new(base=ALL, data=T2);

 

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 271 views
  • 1 like
  • 4 in conversation