BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

Hi Experts,

 

I need your assistance to get some effective steps to fetch the data for backup.

Please have a look on the below given Sample dataset.

 

data Branch1;
input Id r_date date9. val1 val2 val3;
format r_date date9.;
cards;
1 18jan1962 12 58 45
4 12feb1993 87 65 32
7 06mar2010 98 54 12
9 08dec2000 32 54 65
6 24sep1999 78 25 69
3 01jul2009 58 47 14
;
data Branch2;
input Id r_date date9. val1 val2 val3;
format r_date date9.;
cards;
81 04aug2012 42 56 89
81 28nov2011 12 26 39
54 06sep2015 18 19 17
67 08jun2018 37 38 39
67 17feb2019 36 45 11
67 27nov2016 84 95 62
19 10oct1998 27 28 29
56 12feb1997 81 82 83
73 14jan2007 97 91 93
73 01may2016 65 12 98
;

My organization have 20 more branches (Branch1, Branch2, ....)

I need to pull newly entered data to my BckUp table from all branches on every month end.

As of now my backup table have few data from these branches. 

BckUp : 

 

Idr_dateval1val2val3
412-Feb-93876532
706-Mar-10985412
624-Sep-99782569
301-Jul-09584714
8104-Aug-12425689
5406-Sep-15181917
6708-Jun-18373839
1910-Oct-98272829
5612-Feb-97818283
7314-Jan-07979193

 

There is two conditions need to be process: 

1. update my backup table with the newly entered ID's from branches.

2. update my backup table with the existing ID's which not availed r_date from branches.

 

I have a method in SQL but that is not more effective. (It take much time/ long query for all branch).

So I'm looking for a effective Data step methods.

Even if you suggest any code with a macro, It would be more helpful for me on every end of the month.

 

Thanks in advance! 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not really sure I follow you, but I am guessing this bckup dataset should have the branches datasets merged on and any new data added or updated, so perhaps something like;

/* Step 1 set all like data together */
data total;
  set branch:;
run;

/* Step 2 merge the total data to bckup */
/* Assumes both already sorted */
data bckup;
  merge bckup (in=a)
        total (in=b rename=(val1=v1 val2=v2 val3=v3));
  by id r_date;
  if a and b then do;
    flag="Updated";
    val1=v1;
    val2=v2;
    val3=v3;
  end;
  else if a then do;
    flag="No change";
  end;
  else do;
    flag="New data";
    val1=v1;
    val2=v2;
    val3=v3;
  end;
run;

Thats a bit verbose, but trying to show the process clearly.  The in= part is important, as that is how you detemine new/changed.

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not really sure I follow you, but I am guessing this bckup dataset should have the branches datasets merged on and any new data added or updated, so perhaps something like;

/* Step 1 set all like data together */
data total;
  set branch:;
run;

/* Step 2 merge the total data to bckup */
/* Assumes both already sorted */
data bckup;
  merge bckup (in=a)
        total (in=b rename=(val1=v1 val2=v2 val3=v3));
  by id r_date;
  if a and b then do;
    flag="Updated";
    val1=v1;
    val2=v2;
    val3=v3;
  end;
  else if a then do;
    flag="No change";
  end;
  else do;
    flag="New data";
    val1=v1;
    val2=v2;
    val3=v3;
  end;
run;

Thats a bit verbose, but trying to show the process clearly.  The in= part is important, as that is how you detemine new/changed.

Sathish_jammy
Lapis Lazuli | Level 10

Dear @RW9 , @andreas_lds 

 

Thank you so much for your help. Your code works well.

If you wouldn't mind, Can u share this code in Macro format.

 

Much Thanks

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Macro is nothing more than a find replace system which creates text.  I see nothing in your spec which has need of macro code to generate anything.  I mean you could put %macro and %mend around it, but other than making the code obfuscated, messier, and less efficient, you would not achieve anything more than is already achieved.

Sathish_jammy
Lapis Lazuli | Level 10

That’s very kind of you.

Bunch of Thanks for your valuable comments.

andreas_lds
Jade | Level 19

Step 1: append all branch-dataset to form one big dataset (makes further processing easier), Code could be something like:

data all_braches;
  set branch:;
run;

Step 2: Use the update-statement in another data-step (this is non-working code):

data backup;
  update backup all_branches;
  by Id;

  /* more statements for the second condition are required */
run;

Can you post the backup-dataset as data-step? And include at least one example for each condition?

Sathish_jammy
Lapis Lazuli | Level 10
data bckup;
input Id r_date date9. val1 val2 val3;
format r_date date9.; cards; 81 04Aug2012 42 56 89 54 06Sep2015 18 19 17 67 08Jun2018 37 38 39 19 10Oct1998 27 28 29 56 12Feb1997 81 82 83 73 14Jan2007 97 91 93 4 12Feb1993 87 65 32 7 06Mar2010 98 54 12 6 24Sep1999 78 25 69 3 01Jul2009 58 47 14 ; cards; /*need to be update in backup from branches*/ 1 18jan1962 12 58 45 9 08dec2000 32 54 65 81 28nov2011 12 26 39 67 17feb2019 36 45 11 67 27nov2016 84 95 62 73 01may2016 65 12 98

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
  • 6 replies
  • 640 views
  • 0 likes
  • 3 in conversation