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 |
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;
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.