I'm trying to use PROC report to manipulate a dataset where I counted how many patients were abstinent from smoking for a certain number of 'months' (derived from dividing days by 30) . However, I also need to add a column where I find out how many patients were not abstinent from smoking (by subtracting the cumulative total of abstinent patients from the total amount of patients, 234).
I added a 'tmp' variable and initialized it to 0 b/c I know it resets the count, but either I've been staring at this too long or my brain's fried b/c I'm blanking on how to get my desired column:
data imp55a;
set ch4.imp55;
Days_new=input(put(Day_abs,dayssmoke.),3.);
Days_newish=ceil(Day_abs/30);
proc report data=imp55a;
column Days_newish ID new n;
define Days_newish / group 'Month Resumed';
define ID / analysis n 'Not abstinent';
define n / 'Count';
define new / computed 'Remaining abstinent';
compute before Days_newish;
tmp=0;
endcomp;
compute new;
new=234+-1*n;
endcomp;
Thank you guys for your kindness & support---I appreciate it so much!
A computed column can only use variables that have been named before them in the COLUMN statement, so new can't use n as that comes later.
I moved n before new and made it noprint, as it is redundant (same values as not abstinent):
proc report data=imp55a;
column Days_newish ID n new;
define Days_newish / group 'Month Resumed';
define ID / analysis n 'Not abstinent';
define n / noprint;
define new / computed 'Remaining abstinent';
compute before;
all = 234;
endcomp;
compute new;
new = all - n;
endcomp;
compute after days_newish;
all = all - n;
endcomp;
run;
Result:
Month Resumed Not abstinent Remaining abstinent 0 13 221 1 139 82 2 18 64 3 10 54 4 3 51 5 5 46 6 3 43 7 2 41 8 2 39 9 3 36 11 2 34 12 1 33 13 33 0
To see what your code really does, we also ned to see the PROC FORMAT for dayssmoke.
proc format;
value dayssmoke
0-30 ='1'
31-60='2'
61-90 ='3'
91-120='4'
121-150 ='5'
151-180='6'
181-210 ='7'
211-240='8'
241-270 ='9'
271-300='10'
301-330 ='11'
331-365='12'
;
run;
Thank you for your input; I didn't include it because I used the alternative (ceiling/division).
A computed column can only use variables that have been named before them in the COLUMN statement, so new can't use n as that comes later.
I moved n before new and made it noprint, as it is redundant (same values as not abstinent):
proc report data=imp55a;
column Days_newish ID n new;
define Days_newish / group 'Month Resumed';
define ID / analysis n 'Not abstinent';
define n / noprint;
define new / computed 'Remaining abstinent';
compute before;
all = 234;
endcomp;
compute new;
new = all - n;
endcomp;
compute after days_newish;
all = all - n;
endcomp;
run;
Result:
Month Resumed Not abstinent Remaining abstinent 0 13 221 1 139 82 2 18 64 3 10 54 4 3 51 5 5 46 6 3 43 7 2 41 8 2 39 9 3 36 11 2 34 12 1 33 13 33 0
Thank you so much for your help; I appreciate it so much!!! The code worked after I corrected the incorrect order and the incorrect usage of computation variables.
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.