BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

 

View solution in original post

4 REPLIES 4
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).

Kurt_Bremser
Super User

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.

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!

How to Concatenate Values

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.

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
  • 4 replies
  • 642 views
  • 2 likes
  • 2 in conversation