BookmarkSubscribeRSS Feed
Srigyan
Quartz | Level 8

I have the price for 3 product where there is product start date. it means for ID1 from 01-Jan-18 price will be 7.

Product_IDstarting Dateprice
ID101-Jan-187
ID205-Jan-1889
ID303-Jan-186

 

I have another table which overrides the older price. it means for the ID1 price has changed as 12 from 03-Jan-2018 and again on 06-Jan-2018.

 

Product_IDOverridden dateprice
ID103-Jan-1812
ID106-Jan-183
ID107-Jan-185
ID207-Jan-1887
ID210-Jan-1899
ID304-Jan-187
ID305-Jan-185
ID307-Jan-188
ID311-Jan-189

 

 

the output table will be like following...

 

Product_IDDateprice
ID101-Jan-187
ID102-Jan-187
ID103-Jan-1812
ID104-Jan-1812
ID105-Jan-1812
ID106-Jan-183
ID107-Jan-185
ID108-Jan-185
ID109-Jan-185
ID110-Jan-185
ID111-Jan-185
ID1so on till today()5
ID205-Jan-1889
ID206-Jan-1889
ID207-Jan-1887
ID208-Jan-1887
ID209-Jan-1887
ID210-Jan-1899
ID211-Jan-1899
ID212-Jan-1899
ID2so on till today()99
ID303-Jan-186
ID304-Jan-187
ID305-Jan-185
ID306-Jan-185
ID307-Jan-188
ID308-Jan-188
ID309-Jan-188
ID310-Jan-188
ID311-Jan-189
ID3so on till today()9
ID313-Jan-189

 

 

Please share the optimize code... 

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I am not here to type in test data, post test data in the form of a datastep in future, as such this code is not tested:

data total;
  set table1 (rename=(starting=date)) 
        table2 (rename=(overidden=date));
run;
proc sort data=total;
  by product_id date;
run;
data want;
  set total;
  retain lst_date;
  by product_id;
  if first.product_id then do;
    lst_date=date;
    output;
  end;
  else do;
    do i=1 to overidden-lst_date;
      date=date+i;
      output;
    end;
    lst_date=date;
  end;
Srigyan
Quartz | Level 8
do i=1 to overidden-lst_date;

from where you got overidden

Astounding
PROC Star

This may not be the fastest code but it should work:

 

data have1a;

   set have1;

   do date=starting_date to today();

      output;

   end;

   drop starting_date;

run;

 

data have2a;

   set have2;

   do date=overridden_date to today();

      output;

   end;

   drop overridden_date;

run;

 

proc sort data=have2a;

   by product_ID date;

run;

 

data want;

   update have1a have1b;

   by product_ID date;

run;

Srigyan
Quartz | Level 8

the number of overrides are not confirmed, so this one does not fulfill the requirement.

 

Astounding
PROC Star

What are you talking about?  Multiple overrides are part of the incoming data (HAVE2), but the number of overrides does not appear in the final solution data set.  If you try the code on your data, it should work perfectly.  But you have to try it and see.

Ksharp
Super User
data a;
input Product_ID $ startingDate : date11. price;
format 	startingDate date11.;
cards;
ID1	01-Jan-18	7
ID2	05-Jan-18	89
ID3	03-Jan-18	6
;
run;

data b;
input Product_ID $ startingDate : date11. price;
format 	startingDate date11.;
cards;
ID1	03-Jan-18	12
ID1	06-Jan-18	3
ID1	07-Jan-18	5
ID2	07-Jan-18	87
ID2	10-Jan-18	99
ID3	04-Jan-18	7
ID3	05-Jan-18	5
ID3	07-Jan-18	8
ID3	11-Jan-18	9
;
run;
data have;
 set a b;
 by Product_ID  startingDate;
run;
data want;
 merge have have(firstobs=2 drop=price rename=(Product_ID=_Product_ID startingDate=_startingDate)); 
 output;
 if Product_ID=_Product_ID then do;
   do i= startingDate+1 to _startingDate-1;
     startingDate=i;output;
   end;
 end;
 else do;
   do i= startingDate+1 to today();
     startingDate=i;output;
   end;
 end;
 drop _: i;
 run;
Srigyan
Quartz | Level 8

Instead to today I put     startingDate+15;

do i= startingDate+1 to startingDate+15; range is coming for start_date from 1st Jan to 15-Jan is same. though it is correct for override data.

 

Product_IDstartingDateprice
ID101-Jan-187
ID102-Jan-187
ID103-Jan-187
ID104-Jan-187
ID105-Jan-187
ID106-Jan-187
ID107-Jan-187
ID108-Jan-187
ID109-Jan-187
ID110-Jan-187
ID111-Jan-187
ID112-Jan-187
ID113-Jan-187
ID114-Jan-187
ID115-Jan-187
ID116-Jan-187
ID103-Jan-1812
ID104-Jan-1812
ID105-Jan-1812
ID106-Jan-183
ID107-Jan-185
ID108-Jan-185
ID109-Jan-185
ID110-Jan-185
ID111-Jan-185
ID112-Jan-185
ID113-Jan-185
ID114-Jan-185
ID115-Jan-185
ID116-Jan-185
ID117-Jan-185
ID118-Jan-185
ID119-Jan-185
ID120-Jan-185
ID121-Jan-185
ID122-Jan-185
ID205-Jan-1889
ID206-Jan-1889
ID207-Jan-1889
ID208-Jan-1889
ID209-Jan-1889
ID210-Jan-1889
ID211-Jan-1889
ID212-Jan-1889
ID213-Jan-1889
ID214-Jan-1889
ID215-Jan-1889
ID216-Jan-1889
ID217-Jan-1889
ID218-Jan-1889
ID219-Jan-1889
ID220-Jan-1889
ID207-Jan-1887
ID208-Jan-1887
ID209-Jan-1887
ID210-Jan-1899
ID211-Jan-1899
ID212-Jan-1899
ID213-Jan-1899
ID214-Jan-1899
ID215-Jan-1899
ID216-Jan-1899
ID217-Jan-1899
ID218-Jan-1899
ID219-Jan-1899
ID220-Jan-1899
ID221-Jan-1899
ID222-Jan-1899
ID223-Jan-1899
ID224-Jan-1899
ID225-Jan-1899
ID303-Jan-186
ID304-Jan-186
ID305-Jan-186
ID306-Jan-186
ID307-Jan-186
ID308-Jan-186
ID309-Jan-186
ID310-Jan-186
ID311-Jan-186
ID312-Jan-186
ID313-Jan-186
ID314-Jan-186
ID315-Jan-186
ID316-Jan-186
ID317-Jan-186
ID318-Jan-186
ID304-Jan-187
ID305-Jan-185
ID306-Jan-185
ID307-Jan-188
ID308-Jan-188
ID309-Jan-188
ID310-Jan-188
ID311-Jan-189
ID312-Jan-189
ID313-Jan-189
ID314-Jan-189
ID315-Jan-189
ID316-Jan-189
ID317-Jan-189
ID318-Jan-189
ID319-Jan-189
ID320-Jan-189
ID321-Jan-189
ID322-Jan-189
ID323-Jan-189
ID324-Jan-189
ID325-Jan-189
ID326-Jan-189

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1633 views
  • 0 likes
  • 4 in conversation