Solved
New Contributor
Posts: 3

# copy value from periouvs week to the next week

Hi,

I have only beginning inventory for day 4.03.2017 and no ending inventory. I am able to calculate ending inventory for this daybased on the beginning invory for this day and other variables that I have. But here where the problem ibegions....I need  SAS to use calculated ending inventory value from the day 4.03.2017 and have this equal to beginning inventory for dat 4.04.2017 and so on.

I am not sure where toe ven begin.

Thank you,

mag

Accepted Solutions
Solution
‎04-13-2017 04:16 PM
Posts: 3,059

## Re: copy value from periouvs week to the next week

``````prev_ending=lag(ending_inventory);
if missing(beginning_inventory) then beginning_inventory=prev_ending;
``````

Do yourself a huge favor and make any dates numeric and apply datetime formats to them, rather than having dates as character variables.

--
Paige Miller

All Replies
Super User
Posts: 13,583

## Re: copy value from periouvs week to the next week

It really helps to provide some example data and the desired result from that sample data.

Also, are your dates actual SAS date values or character?

New Contributor
Posts: 3

## Re: copy value from periouvs week to the next week

Hi,

The formats:  plan-character, week-character, beginning_inventory, usage and orders are nuemrical.

Example of data:

Plant        Week                  beginning_Inventory   usage    orders                     ending inventory (has to be calculated....)

h               01/14/2017        30                                 5            22

a               01/21/2017       .

b               01/29/2017       .

I need to calculate ending inventory:   ending_inventory=beginning_inventory-usage+orders.

So for the first week it is easy to get ending_inventory whihc is 30-5+22=47  now this ending inventory from the week 01/14/2017 needs to become my beginning inventory for the week 01/29/2017 an so on...

Any idea how to approach this problem?

Thank you so much.

Solution
‎04-13-2017 04:16 PM
Posts: 3,059

## Re: copy value from periouvs week to the next week

``````prev_ending=lag(ending_inventory);
if missing(beginning_inventory) then beginning_inventory=prev_ending;
``````

Do yourself a huge favor and make any dates numeric and apply datetime formats to them, rather than having dates as character variables.

--
Paige Miller
New Contributor
Posts: 3

## Re: copy value from periouvs week to the next week

Thank you. It worked.
☑ This topic is solved.