DATA Step, Macro, Functions and more

Calculations in SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

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.

 

 

IDday weight  
25110 
25210.6 
25310.9 
25411.3 
25512 
25612.4 
25712.3 
25813 
25913.5 
251014.14.1
5819.8 
58210.2 
58310.6 
58411.5 
58511.8 
58612.8 
58712.9 
58813.5 
58913.9 
581014.64.8

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

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;

View solution in original post


All Replies
Super User
Posts: 11,343

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: 19,769

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
  • 267 views
  • 1 like
  • 3 in conversation