BookmarkSubscribeRSS Feed
Longimanus
Obsidian | Level 7

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.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Longimanus
Obsidian | Level 7

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