BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Spintu
Quartz | Level 8

How to add flag  if the condition is true in same group.

 

The below table as an example with in vendor_id group if the community is 'Y' then I need a new add variable flag as "Y"

Pract_idVendor_IDCommunity
11101 
12101Y
13101 
14102 
15102 
16103 
17103Y
18104Y

 

Want output:

Pract_idVendor_IDCommunityCBF
11101 Y
12101YY
13101 Y
14102  
15102  
16103 Y
17103YY
18104YY
I am trying for this code. However it is not working.

proc sql;
	create table want as
		select *,'Y' as CBF
			from have
				group by Vendor_ID
					having Community='Y'
						order by vend_bsid;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ
data have;
infile datalines missover;
input Pract_id	Vendor_ID	Community $;
datalines;
11	101	 
12	101	Y
13	101	 
14	102	 
15	102	 
16	103	 
17	103	Y
18	104	Y
;

proc sql;
	create table 	want as
		select
					a.*,
					b.cbf
		from
					have as a
						left join
					(
						select
									distinct vendor_id as vendor_id_d,
									community as cbf
						from
							 		have
						where
									community = 'Y'
					) as b
						on	a.vendor_id = b.vendor_id_d;
quit;

maguiremq_0-1656679491332.png

 

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

UNTESTED CODE

proc freq data=have;
    by vendor_id;
    table community/out=_counts_;
run;
data want;
    merge have _counts_(rename=(community=community1) where=(community1='Y'));
    by vendor_id;
    if count>=1 then CBF='Y';
    drop community1 count percent;
run;

 

If you want tested code, please provide data as SAS data step code (example)(instructions). We can't write code to work from screen captures.

--
Paige Miller
Spintu
Quartz | Level 8
Thank You. I tested this is working fine.
maguiremq
SAS Super FREQ
data have;
infile datalines missover;
input Pract_id	Vendor_ID	Community $;
datalines;
11	101	 
12	101	Y
13	101	 
14	102	 
15	102	 
16	103	 
17	103	Y
18	104	Y
;

proc sql;
	create table 	want as
		select
					a.*,
					b.cbf
		from
					have as a
						left join
					(
						select
									distinct vendor_id as vendor_id_d,
									community as cbf
						from
							 		have
						where
									community = 'Y'
					) as b
						on	a.vendor_id = b.vendor_id_d;
quit;

maguiremq_0-1656679491332.png

 

Ksharp
Super User
data have;
infile datalines missover;
input Pract_id	Vendor_ID	Community $;
datalines;
11	101	 
12	101	Y
13	101	 
14	102	 
15	102	 
16	103	 
17	103	Y
18	104	Y
;

proc sql;
	create table 	want as
select *,max(Community) as CBF  from have group by Vendor_ID order by 1;
quit;
Tom
Super User Tom
Super User

You can just remerge the data with the records where the existing flag is true.

 

data have;
  length Vendor_ID Pract_id 8 Community $1;
  input Pract_id	Vendor_ID	Community ;
datalines;
11	101	. 
12	101	Y
13	101	. 
14	102	. 
15	102	. 
16	103	. 
17	103	Y
18	104	Y
;

data want;
   merge have 
         have(keep=vendor_id community rename=(community=CBF) where=(CBF='Y'))
   ;
   by Vendor_id;
run;

proc print;
run;
       Vendor_
Obs       ID      Pract_id    Community    CBF

 1       101         11                     Y
 2       101         12           Y         Y
 3       101         13                     Y
 4       102         14
 5       102         15
 6       103         16                     Y
 7       103         17           Y         Y
 8       104         18           Y         Y

 

tarheel13
Rhodochrosite | Level 12

I see you already have solution but I would've done it with retain statement. 

proc sort data=have;
    by vendor_id descending community;
run;

data have2;
    set have;
    by vendor_id descending community;
    retain cbf;
    if first.vendor_id then cbf='';
    if first.vendor_id and community='Y' then cbf='Y';
run;
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
  • 9 replies
  • 2254 views
  • 6 likes
  • 6 in conversation