BookmarkSubscribeRSS Feed
OlsabeckT29
Fluorite | Level 6

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

3 REPLIES 3
data_null__
Jade | Level 19

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;

Capture.PNG

PaigeMiller
Diamond | Level 26

Are there ever more than two years for January? Are there ever more than two years for other months? What would we do then?

--
Paige Miller
OlsabeckT29
Fluorite | Level 6
No, we are only looking at the difference between two years.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 178 views
  • 0 likes
  • 3 in conversation