A loop would be useful if you had to do the same process to a set of variables. But you have only one variable (totalsales), and you want to compare successive observations within the data set:
Data All;
Input Department$ Totalsales Date DDMMYY8.;
format Date yymmdd10.;
cards;
D 45 01/03/19
D 47 02/03/19
D 51 03/03/19
D 55 04/03/19
B 35 01/03/19
B 37 02/03/19
B 43 03/03/19
B 42 04/03/19
E 59 03/03/19
E 64 04/03/19
M 45 01/03/19
M 50 02/03/19
M 55 03/03/19
M 56 04/03/19
M 59 05/03/19
M 59 07/03/19
M 59 06/03/19
run;
data want;
set all;
by department notsorted;
newsales=dif(totalsales);
if first.department then newsales=.;
run;
The DIF function is defined as dif(x)=x-lag(x). The "if first.department …" statement tests for the start of a new department, and set newsales to missing, thereby avoiding contamination of the start of one department with a lagged value from the prior department.
The "notsorted" option is because you seem to have your data observations grouped by department, but not in ascending order.