BookmarkSubscribeRSS Feed
Veeresh
Fluorite | Level 6

 

 

Help me on this below scenario.

 

output requirements

1)flag variable-  Flag on Countries IND and CHN. 

 

If country IND then for first occurrence its should flag as 1 and if same data available in next occurrence  that should flag it as

If same data chain got dis continued then again if we find IND in country column again it flags first data occurrence as 1 and remain 0. 

 

Similarly 

 

If country CHN then for first occurrence its should flag as 0 , if same data available in next occurrence  that should flag it as 0  and only flag 1 for last occurrence of CHN before dis continue the same data .

If same data chain got dis continued then again if we find CHN in country column again it flags first data occurrence as 0 and next occurrence 1

 

2)Count variable-  cumulative count .

 

data

Country

IND

IND

ENG

CHN

CHN

CHN

IND

IND

IND

USA

PAK

CHN

CHN

 

Expected output 

count Country FLAG
1          IND       1
1          IND       0
2          ENG     0
3          CHN     0
3          CHN     0
3          CHN     1
4          IND      1
4         IND       0
4         IND       0
5        USA       0
6        PAK       0
7       CHN       0
7       CHN      1

1 REPLY 1
ed_sas_member
Meteorite | Level 14

Hi @Veeresh 

 

Here is a way to do that:

data have;
	input Country $;
	datalines;
IND
IND
ENG
CHN
CHN
CHN
IND
IND
IND
USA
PAK
CHN
CHN
;

data want;
	set have;
	by country notsorted;
	
	if 		first.country and country = 'IND' then flag = 1;
	else if last.country and country = 'CHN' then flag = 1;
	else 	flag = 0;

	if first.country then count+1;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 500 views
  • 1 like
  • 2 in conversation