DATA Step, Macro, Functions and more

I want to Substract row wise only if all values in columns Region, country,product,subproduct match

Reply
Contributor
Posts: 48

I want to Substract row wise only if all values in columns Region, country,product,subproduct match

Super User
Posts: 11,343

Re: I want to Substract row wise only if all values in columns Region, country,product,subproduct m

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

 

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

One approach migh be to sort by these values so they grouped, possibly within some other value such as a date. Then use by group with first. subproduct logic.

 

As always, showing some example data and the desired result for that data helps. See the instructions above. Include examples where the subtraction should not take place and some where it should, 2 or 3 of each should be sufficient to get started. If you have a lot of variables to "subtract" then only need to show one or two as the approach should be basically the same.

PROC Star
Posts: 283

Re: I want to Substract row wise only if all values in columns Region, country,product,subproduct m

@subrat1  If you attach file, honestly I don't think it is convenient for any of us to help. Super user @RW9 has often many a time wrote requesting people to post in the form of a datastep and I wonder not many people are doing that.

 

Anyway, the below code works for your sample:

 

 

data want;

if (_n_ = 1) then do;

   if 0 then set have;

    declare hash myhash(dataset: "have(where=(Scenario='BUDGET'))");

    myhash.definekey('Region','Country','Product','SubProduct','Quarter' );

    myhash.definedata('Gross_Premium_Written','Net_Premium_Written');

    myhash.definedone();

 end;

 set have(where=(Scenario='ACTUALS') rename=(Gross_Premium_Written=_Gross_Premium_Written Net_Premium_Written=_Net_Premium_Written));

 rc=myhash.find();

if rc=0 then do;

Scenario='DELTA';

Gross_Premium_Written=_Gross_Premium_Written-Gross_Premium_Written;

Net_Premium_Written=_Net_Premium_Written-Net_Premium_Written;

output;

end;

drop rc _:;

run;

 

 

 

 

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