BookmarkSubscribeRSS Feed
Longimanus
Quartz | Level 8

Hi there!

Getting a little rusty with coding since I spend too much time in DIS. 😉 I have the following problem (only because that's the way I chose to solve the task I have to fix). Smiley Happy

I am reading a dataset_1 (account = key) and depending on the value of some variables on certain observations I need to check another dataset_2 if this account is found there.

If so, I need to do some calculations for this account and finish with deleteíng the observation i dataset-2.

Does this makes sense? I tried with a macro but that didn't work. Not the way I used it anyway. Smiley Wink

Who likes to take up the challenge?

Kind regards,

Menno :smileycool: 

3 REPLIES 3
Patrick
Opal | Level 21

If I understand this right then it is about "how to implement conditional processing using DIS?".

What I've done in the past is to start a macro definition in the pre-code of the first node in the chain for conditional processing and then in the post-code of the last node for conditional processing end the macro and call it. If you need this more visible in the flow then you could also add user written code nodes at the beginning and the end of this sub-flow.

So "%macro DoSomething; %if <condition> %then %do;" in the pre-code of the first node, and "%end; %mend; %DoSomething;" in the post-code of the last node.

Reading Quentin's answer I've realised that you're eventually just after some logic of how to code this. Under the assumption that your tables are stored in SAS below some code which is hopefully useful for you.

data dataset_1;
  do account_nr=10 to 15;
    output;
  end;
run;

data dataset_2;
  do account_nr=7,11,12,15,22;
    output;
  end;
run;

data dataset_1(drop=_:);
  set dataset_1 end=last;

  if _n_=1 then
    do;
      declare hash h1 (dataset:'dataset_2');
      _rc=h1.defineKey('account_nr');
      _rc=h1.defineDone();
      declare hash Hdel ();
      _rc=Hdel.defineKey('account_nr');
      _rc=Hdel.defineDone();
    end;

  if h1.check()=0 then
    do;
      /* here some calculations */
      calc_var=_n_;
      /* collect account_nr to be deleted */
      _rc=Hdel.add();
    end;

  if last then
    do;
      _rc=Hdel.output(dataset:'work.deletes');
    end;
run;

proc sql;
  delete from dataset_2
  where account_nr in (select account_nr from deletes)
  ;
quit;

Quentin
Super User

I'm not sure I follow.  If you are planning to write the code yourself, you might code it as:

1. Subset the dataset_1 to identify all the account numbers you want to check for in dataset_2.

2. Merge the subset-ed dataset_1 and dataset_2 to identify which of those accounts exist in dataset_2, then do your calculcations / deletetions etc

Doesn't sound like a macro or %INC problem to me.  Sounds more like SQL or DATA step progrmming.  If you are having problems with the programming, might help to show a few sample records from dataset_1 and dataset_2, and describe your desired output.

HTH,

--Q.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Longimanus
Quartz | Level 8

Quentin and Patrick! Thanx for your input! I will try to see if I can use the information/examples you sent me. A SAS consultant one told me I should look into hash coding. Didn't manage to get there yet. Maybe about time ...

If I cannot make it work I will send you the programs I wrote and tell you what output I look for. For now, once again: THANX! :smileygrin:

/ Menno (a.k.a. Longimanus)

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 765 views
  • 0 likes
  • 3 in conversation