BookmarkSubscribeRSS Feed
2 REPLIES 2
ballardw
Super User

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.

novinosrin
Tourmaline | Level 20

@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;

 

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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