## How to calculate lag date according to different category of a variable

Occasional Contributor
Posts: 14

# How to calculate lag date according to different category of a variable

Hi guys,

I am working on a data-set containing multiple row for each studycode. there are other variables namely atc_code and date_of_supply. I want to indicate those studycodes whose consecutive date_of _supply for a specific atc group (several atc_code constitutes a group. e.g: C100BS, C11BD4, C10AA01, C112KN CONSTITUTES ONE ATC GROUP ) is less than say 60 days.

I WANT IT ALL IN A SINGLE FILE. CAN ANYONE HELP ME OUT? SAMPLE XLS IS ATTACHED

PROC Star
Posts: 1,351

## Re: How to calculate lag date according to different category of a variable

Can you please paste the sample as text here and also a sample of your want.I would like a sample of your wanted output. Thank you!

Frequent Contributor
Posts: 109

## Re: How to calculate lag date according to different category of a variable

[ Edited ]

This code will work, if not try to debug it, there might be some problem with position of retain variable.

``````proc sort data=sheet3;
by studycode atc_codes date_of_supply;
run;

data final_extract(drop=date_of_supply1);
set sheet3;
retain date_of_supply1;
by studycode atc_codes;
if first.atc_codes then do;
diff=0;
date_of_supply1=date_of_supply;
end;
else do;
diff=date_of_supply1
date_of_supply1=date_of_supply;
if diff ge 60 then output;
end;
run;``````
Super User
Posts: 13,084

## Re: How to calculate lag date according to different category of a variable

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Occasional Contributor
Posts: 14

Many Thanks

PROC Star
Posts: 2,231

## Re: How to calculate lag date according to different category of a variable

@ballardw This whole text should be on the "create post" page.

PROC Star
Posts: 2,231

## Re: How to calculate lag date according to different category of a variable

@ChrisHemedinger Is it possible to add this text directly as part of the "New post" page? Most posted data is still in the wrong format.

Community Manager
Posts: 3,362

## Re: How to calculate lag date according to different category of a variable

@ChrisNZ We'll look into it, especially as we work on our project for creating a more responsive-friendly design.

Discussion stats
• 7 replies
• 175 views
• 3 likes
• 6 in conversation