BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kran
Obsidian | Level 7

Hi ,

I have a dataset as below -

 

ShipmentID     ShipmentType   Customer     From          To           Priceperpackagein$    PriceofShipmentin$    

        1                         A                    X            NYC        BOS                     5                                   -

        1                         A                    X            NYC        BOS                     7                                   -

        2                         B                    Y            NYC        DEN                     -                                   30

        2                         B                    Y            NYC        DEN                     -                                   30

        3                         C                    Z            NYC        CHI                      -                                   25

        3                         C                    Z            NYC        CHI                      -                                   25

        4                         A                    W           NYC        BOS                     5                                   -

        4                         A                    W           NYC        BOS                     7                                   -

        5                         D                    X            NYC        BOS                     5                                   -

        5                         D                    X            NYC        BOS                     7                                   -

        6                         E                    Y            NYC        DEN                     5                                   -

        6                         E                    Y            NYC        DEN                     7                                   -

 

So here we see that Shipment types A, D & E are priced per package in the shipment, while types B & C are priced for the whole shipment. (to be noted : the pricing done for shipment visible in each row is a repeat value, whereas the package prices are individual and are to be summed up)

 

So I guess you see where I'm going..I want to add a column on the extreme right end named ..'TotalPricein$'

and put in a condition

for each ShipmentID, if ShipmentType  is A or D or E,

then value of TotalPricein$ = sum of Priceperpackagein$

else value of TotalPricein$ = PriceofShipmentin$

 

something along these lines..

 

the prob is that the criteria for values to be summed up are in diff rows and columns....so need help there with the logic..

perhaps in two table transformations?

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Can you post the output ?

 

data have;
input ShipmentID     ShipmentType  $ Customer   $  From    $   To $     Priceperpackagein   PriceofShipmentin;
cards;
        1                         A                    X            NYC        BOS                     5                                   .
        1                         A                    X            NYC        BOS                     7                                   .
        2                         B                    Y            NYC        DEN                     .                                   30
        2                         B                    Y            NYC        DEN                     .                                   30
        3                         C                    Z            NYC        CHI                      .                                   25
        3                         C                    Z            NYC        CHI                      .                                   25
        4                         A                    W           NYC        BOS                     5                                   .
        4                         A                    W           NYC        BOS                     7                                   .
        5                         D                    X            NYC        BOS                     5                                   .
        5                         D                    X            NYC        BOS                     7                                   .
        6                         E                    Y            NYC        DEN                     5                                   .
        6                         E                    Y            NYC        DEN                     7                                   .
;
run;

proc sql;
create table want as
 select *,coalesce(sum(Priceperpackagein),PriceofShipmentin) as total
  from have 
   group by shipmentid;
quit;

View solution in original post

2 REPLIES 2
Ksharp
Super User

Can you post the output ?

 

data have;
input ShipmentID     ShipmentType  $ Customer   $  From    $   To $     Priceperpackagein   PriceofShipmentin;
cards;
        1                         A                    X            NYC        BOS                     5                                   .
        1                         A                    X            NYC        BOS                     7                                   .
        2                         B                    Y            NYC        DEN                     .                                   30
        2                         B                    Y            NYC        DEN                     .                                   30
        3                         C                    Z            NYC        CHI                      .                                   25
        3                         C                    Z            NYC        CHI                      .                                   25
        4                         A                    W           NYC        BOS                     5                                   .
        4                         A                    W           NYC        BOS                     7                                   .
        5                         D                    X            NYC        BOS                     5                                   .
        5                         D                    X            NYC        BOS                     7                                   .
        6                         E                    Y            NYC        DEN                     5                                   .
        6                         E                    Y            NYC        DEN                     7                                   .
;
run;

proc sql;
create table want as
 select *,coalesce(sum(Priceperpackagein),PriceofShipmentin) as total
  from have 
   group by shipmentid;
quit;
Kran
Obsidian | Level 7

Gosh, I salute you for your help! Thanks a ton

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2572 views
  • 2 likes
  • 2 in conversation