Macro? Include a program?

Reply
Contributor
Posts: 26

Macro? Include a program?

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: 

Respected Advisor
Posts: 4,173

Re: Macro? Include a program?

Posted in reply to Longimanus

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=_Smiley Happy;
  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;

PROC Star
Posts: 1,322

Re: Macro? Include a program?

Posted in reply to Longimanus

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.

Contributor
Posts: 26

Re: Macro? Include a program?

Posted in reply to Longimanus

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)

Ask a Question
Discussion stats
  • 3 replies
  • 181 views
  • 0 likes
  • 3 in conversation