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

I have two tables A and B.

 

Table A has Id, Item_Name, price_start_date, new_price_end_date

 

Table B has Covers, Date.

 

I want to create a code to add all the covers in Table A which lie in between Price_start_date and new_price_end_date (that is sum of covers between the dates in Table B).

 

I wrote a code to 

 

Proc SQL;

create table work.covers_before_total as
Select a.*, Sum(Case When a.Price_start_Date <= a.NEW_Price_End_Date and a.Price_start_Date <= b.date and a.NEW_Price_End_Date >= b.date then b.Covers Else 0 END) as covers
from work.covers_before a,work.covers_measurement_model b

group by a.id

Run;

 

But the sum gives out 0 for some reason. 

 

Is there also a better way of doing it?

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Your code (once modified to meet what proc sql expects) appears to work correctly. I ran:

Proc SQL;
  create table work.covers_before_total as
    Select a.*, Sum(Case When a.Price_start_Date <= a.NEW_Price_End_Date and
                              a.Price_start_Date <= b.date and
                              a.NEW_Price_End_Date >= b.date then b.Covers Else 0 END) as covers
      from work.covers_before a,work.covers_measurement_model b
        group by a.id
  ;
quit;

Can't help further unless/until you provide some example data for the two datasets, and the result you expect.

 

Art, CEO, AnalystFinder.com

 

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

Your code (once modified to meet what proc sql expects) appears to work correctly. I ran:

Proc SQL;
  create table work.covers_before_total as
    Select a.*, Sum(Case When a.Price_start_Date <= a.NEW_Price_End_Date and
                              a.Price_start_Date <= b.date and
                              a.NEW_Price_End_Date >= b.date then b.Covers Else 0 END) as covers
      from work.covers_before a,work.covers_measurement_model b
        group by a.id
  ;
quit;

Can't help further unless/until you provide some example data for the two datasets, and the result you expect.

 

Art, CEO, AnalystFinder.com

 

hvempati
Obsidian | Level 7
Hi, @art297 Thank you for your reply. I had figured out that the input data I was giving was incorrect.

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