Hello, I am trying to calculate the difference between the data observed in 2023 and 2024 by month. I have a total of 84 observations and need to calculate the difference between all 12 months. Below is a sample of how the data is formatted.
data Have;
input id 23Jan 23Feb 23Mar 24Jan 24Feb 24Mar;
datalines;
1 0.54 0.54 0.52 0.62 0.61 0.60
2 0.48 0.45 0.48 0.71 0.68 0.67
3 0.59 0.61 0.59 0.57 0.54 0.58
4 0.61 0.62 0.59 0.62 0.61 0.54
;
I want a dataset that contains the difference for each month for all 84 observations.
Data Want;
ID JanDiff FebDiff MarDiff
1
2
3
4
I know I can write out all 12 calculations but is their a more efficient coding strategy to calculate the differences?
Thanks in advance
Several variables suggest an array might be useful.
data Have;
input id Y23Jan Y23Feb Y23Mar Y24Jan Y24Feb Y24Mar;
Array _23 Y23:;
array _24 Y24:;
array _df Dif1-Dif3;
do over _23;
_df = _24-_23;
end;
datalines;
1 0.54 0.54 0.52 0.62 0.61 0.60
2 0.48 0.45 0.48 0.71 0.68 0.67
3 0.59 0.61 0.59 0.57 0.54 0.58
4 0.61 0.62 0.59 0.62 0.61 0.54
;;;;
run;
proc print;
run;
Are there ever more than two years for January? Are there ever more than two years for other months? What would we do then?
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.