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 ;

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
  • 3 replies
  • 537 views
  • 0 likes
  • 3 in conversation