BookmarkSubscribeRSS Feed
Siddharth123
Obsidian | Level 7

Hi All,

 

I am not getting expected results when using INDEXC (please see code below). I would expect all records in the dataset UnSure rather than in For_Sure -

 

DATA TEST;
INPUT Name $50.;
INFILE DATALINES;
DATALINES;
DREFT ESTATES LTD
POTTERSLANE LTD
;
RUN;


DATA For_Sure UnSure;
SET TEST;
IF indexc(Name, "GOVERNMENT") > 0
OR indexc(Name, "COLLEGE") > 0 then OUTPUT For_Sure;
ELSE  OUTPUT UnSure;
RUN;

 

Any help will be greatly appreciated.

 

Thank you

SK

2 REPLIES 2
Siddharth123
Obsidian | Level 7

Hi All,

 

I figured out that I need to use INDEX rather than INDEXC to solve this problem.

 

Kind regards

 

SK

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, function indexc() searchs for one single character in the string, where index() searchs for the whole text.  You can compress your code somewhat, if <> is looking for a binary 1 or 0 result, so index() or findw() (which i show below) will return 0 if not present, so 0 result, and anything is positive and considered true (also, please dont code all in capitals, it makes reading it very hard):

data test;
  input name $50.;
datalines;
DREFT ESTATES LTD
POTTERSLANE LTD
;
run;

data for_sure unsure;
  set test;
  if findw(name,"GOVERNMENT") or findw(name,"COLLEGE") then output for_sure;
  else output unsure;
run;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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