Calculations in SAS

Solved
Occasional Contributor
Posts: 17

Calculations in SAS

[ Edited ]

I have a large data set that contains ID, day, and weight. I need to determine the difference between starting and ending weight for each ID. I have been working with IF statements to try to conquer this but have been unsuccessful. For example, I want to tell SAS that "if" day = 10 then take the corresponding weight - day 1 weight. I have an example of what my output from SAS currently looks like.

Thanks for your help.

 ID day weight 25 1 10 25 2 10.6 25 3 10.9 25 4 11.3 25 5 12 25 6 12.4 25 7 12.3 25 8 13 25 9 13.5 25 10 14.1 4.1 58 1 9.8 58 2 10.2 58 3 10.6 58 4 11.5 58 5 11.8 58 6 12.8 58 7 12.9 58 8 13.5 58 9 13.9 58 10 14.6 4.8

Accepted Solutions
Solution
‎09-23-2016 03:41 PM
Super User
Posts: 23,724

Re: Calculations in SAS

``````data want;
set have;
by id;
retain first_weight;
if first.id then first_weight=weight;
if last.id then diff=weight-first_weight;
run;``````

All Replies
Super User
Posts: 13,542

Re: Calculations in SAS

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.

The instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to create data step code you can post to the forum either by pasting or attaching as a TEXT file.

Solution
‎09-23-2016 03:41 PM
Super User
Posts: 23,724

Re: Calculations in SAS

``````data want;
set have;
by id;
retain first_weight;
if first.id then first_weight=weight;
if last.id then diff=weight-first_weight;
run;``````
☑ This topic is solved.

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

Discussion stats
• 2 replies
• 331 views
• 1 like
• 3 in conversation