BookmarkSubscribeRSS Feed
angeliquec
Quartz | Level 8

Hi, In my "HAVE" dataset, if "var1" is not in the string "not_in", I'd like to tag it as "Y", as in the the "WANT" dataset. 

I need help to produce my "WANT" dataset.

 

data have;
input var1 $ not_in $;
cards;
A A,B,C
D A,B,C
E B,D,A
;
run;

 

Thus, for the first row, since A is in A,B,C, then I'd like its not_in_flg to be "N". 

For the second row, since D is not in A,B,C, then is not_in_flg is to be "Y".

 

data want;
input var1 $ not_in $ not_in_flg $;
cards;
A A,B,C N
D A,B,C Y
E B,D,A N
;
run;

 

4 REPLIES 4
WarrenKuhfeld
Rhodochrosite | Level 12
data have;
input var1 $ not_in $;
not_in_flg = ifc(index(not_in, trim(var1)), 'N', 'Y');
cards;
A A,B,C
D A,B,C
E B,D,A
;
proc print; run;

The last row of your example is wrong.

Reeza
Super User

Is your data always a single character? If not use FINDW or INDEXW to search for a word and return it. Make sure to look at the later parameters for those functions to set them to ignore case or use UPCASE/LOWCASE to make everything the same case.

SuryaKiran
Meteorite | Level 14

Hi,

 

Use find function.

 

data have;
input var1 $ not_in $;
If FIND(not_in,STRIP(var1),'i')=0 then flag="Y";
ELSE Flag="N";
cards;
A A,B,C
D A,B,C
E B,D,A
;
run;
Thanks,
Suryakiran
PGStats
Opal | Level 21

data want;
set have;
if findw(not_in, var1," ,","ti") then notInFlg = "N";
else notInFlg = "Y";
run;
PG

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
  • 4 replies
  • 1025 views
  • 0 likes
  • 5 in conversation