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?
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.