## Processing accross Observations

Solved
Occasional Contributor
Posts: 9

# Processing accross Observations

Dear All,

I am currently building a programm to calculate stock projection, I have the following dataset:

Material Time Consumption OpenningStock Closingstock TargetStock

A            Jan          10                   0                   0                    30

A            Feb          11                   X                   X                    30

A            Mar          13                   X                   X                    30

A            Apr           10                  X                   X                     30

B            Jan

B            Feb

B            .

B            .

I want to code the following rule : Openning Stock Feb = closing Stock Jan and so on (i.e  Openning Stock Month n = closing stock Month n-1)

I would be gratefull if someone could help me to solve the problem.

Thank you.

Best regards.

Accepted Solutions
Solution
‎09-02-2013 06:04 AM
Super User
Posts: 5,884

## Re: Processing accross Observations

First, it would be easier if you transform your Time to a SAS date value, so you can use SAS date functions such as INTNX for your logic.

You could use data step, using BY and first./last.-logic, and have the previous observations value retained (or get it using the lag() function).

Data never sleeps

All Replies
Solution
‎09-02-2013 06:04 AM
Super User
Posts: 5,884

## Re: Processing accross Observations

First, it would be easier if you transform your Time to a SAS date value, so you can use SAS date functions such as INTNX for your logic.

You could use data step, using BY and first./last.-logic, and have the previous observations value retained (or get it using the lag() function).

Data never sleeps
Occasional Contributor
Posts: 9

## Re: Processing accross Observations

Hi LinusH,

thank you for your help,

the time column is already in SAS date format

I used the lag function it is working fine

thank you very much.

Regards.

Occasional Contributor
Posts: 9

## Re: Processing accross Observations

Hi LinusH,

thank you for your help,

could you please provide an example of the code using the Retain function?

thank you very much.

Regards.

Respected Advisor
Posts: 3,852

## Re: Processing accross Observations

RETAIN is not a function.

Frequent Contributor
Posts: 120

## Re: Processing accross Observations

Hi,

For Retain statement, you can refer given document :

http://support.sas.com/publishing/pubcat/chaps/58176.pdf

Regards

Uma Shanker

Occasional Contributor
Posts: 9

## Re: Processing accross Observations

Posted in reply to umashankersaini

thank you,one last question

could please help me with the following problem:

I want to create a code to sum up the three next months(observations) by Material: exemple:

Material               Time               Value          Solution

x                  Jan2013          12                    26 =10+11+5

x                  Feb2013          10                    46=11+5+30

x                  Mar2013          11                    35=5+11

x                  Apr2013            5                    30

x                  May2013          30

y                  Jan2013          12                    26 =10+11+5

y                  Feb2013          10                    46=11+5+30

y                  Mar2013          11                    35=5+11

y                  Apr2013            5                    30

y                  May2013          30

Could you please help.

thank you.

regards.

Tou

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 6 replies
• 274 views
• 3 likes
• 4 in conversation