BookmarkSubscribeRSS Feed
d0816
Quartz | Level 8
IDTypeHas_A1_in_Type
1A1 A3   A1
2A1 A3   A1
3A1 A3   A1
4A1 A3   A1
5A1 A3   A1
6A1 A3   A1
7   
8A3      
9A1 A3   A1
10A3      
11A1 A3   A1
12A3 A1   A1
13A2 
14A3      
15A1   A1

 

In this table I want to create a third column which will be A1 if 2nd column has A1 anywhere or else blank . Any one has any idea how to do this? I used index function but did not work.

 

Thank you.

2 REPLIES 2
novinosrin
Tourmaline | Level 20

data have;
infile cards truncover;
input ID	Type & $;
cards;
1	A1 A3   	A1
2	A1 A3   	A1
3	A1 A3   	A1
4	A1 A3   	A1
5	A1 A3   	A1
6	A1 A3   	A1
7	  	 
8	A3     	 
9	A1 A3   	A1
10	A3     	 
11	A1 A3   	A1
12	A3 A1   	A1
13	A2	 
14	A3     	 
15	A1   	A1
;

data want;
set have;
Has_A1_in_Type=ifc(findw(type,'A1')>0,'A1',' ');
run;
ballardw
Super User

In a data step

 

if index(type,'A1')> 0 then Has_A1_in_type='A1';

 

However I might suggest if you are looking to count the number of times A1 occurs that instead of a character value of A1 that you use a numeric value such as

 

Has_A1= index(type,'A1')>0;

which will have a value of 0 when not present. Then summing on Has_A1 gives you count, mean a percentage.

 

And show the code and values that index does not work with. If you have values like A10 then you likely want INDEXW not index.

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