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 0  . 

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;

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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