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

If the first row for colon_res_bill=1, then I want all of the other colon_res_bill's for that study_id to also be 1.
How would I go about this?

 

This is what I have tried, but it is not working.

proc sort data=colon_auth_paid	out=colon_auth_paid_sort;
	by study_id descending colon_res_bill;
run;

data colon_paid_heirarchy1;
	set colon_auth_paid_sort;
	by study_id descending colon_res_bill;
		if first.colon_res_bill=1 then colon_res_bill_final=1;
		else colon_res_bill_final=0;
run;

 

 

colon res bill.png

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

In the future, please include data as SAS data step code. Do NOT include data as a screen capture.

 

This solution assumes the data set is properly sorted.

 

data want;
    retain flag;
    set have;
    by study_id;
    if first.study_id then flag=(colon_res_bill=1);
    if flag=1 then colon_res_bill=1;
    drop flag;
run;

 

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

In the future, please include data as SAS data step code. Do NOT include data as a screen capture.

 

This solution assumes the data set is properly sorted.

 

data want;
    retain flag;
    set have;
    by study_id;
    if first.study_id then flag=(colon_res_bill=1);
    if flag=1 then colon_res_bill=1;
    drop flag;
run;

 

 

--
Paige Miller
A_Halps
Obsidian | Level 7
This works!!
Thank you for the answer and advice!
AMSAS
SAS Super FREQ

Hi A_Halps

This can be achieved with the FIRST. & LAST. processing and the retain statement 

Here's an example 

 

data input ;
 	drop i ;
	do study_id=1 to 3 ;
		do i=1 to 5 ;
			if i=1 and ranuni(1)>0.5 then
				colon_res_bill=1 ;
			else
				colon_res_bill=0 ;
			output ;
		end ;
	end ;
run ;

proc sort data=input out=srtd ;
	by study_id ;
run ;

data results ;
	retain retain_crb 0 ;
	drop retain_crb ;
	set srtd ;
	by study_id ;
	if first.study_id then
		retain_crb=colon_res_bill ;
	if retain_crb=1 then
		colon_res_bill=1 ;
	output ;
	if last.study_id then	
		retain_crb=0 ;
run ;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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