BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I don't understand why the INDEX function does not work the way it is supposed to. My string that is being searched is a list of grades a given program is offered to (prgoffered) such as "789" or "7" etc. (I have it saved as a character var). And then for each observation I am checking if a given student is in a grade that offers the program. Again, it is saved as a character var. My code is as follows: servedgr=indexc(prgoffered,grade); but when I print the output I see that index put a servedgr=2 when my prgoffered="7" and grade="2". Why isn't my servedgr=0 ????
4 REPLIES 4
LawrenceHW
Quartz | Level 8
Hi,

What is the code you're using? It's a little unclear from your question.

I've just run this code:

DATA test;
prgoffered="789";
grade="2";
servedgr=indexc(prgoffered,grade);
RUN;

And SERVEDGR=0 (as you would expect).

Is this what you were trying?

Cheers,
Lawrence
deleted_user
Not applicable
Hi Lawrence,
Thanks for your response. Indeed, when I ran the code as you said it does work but when I ran my code for some reason it does not! I am not sure why!
here is my code:

data elem;
set elem; /*briging the data set with student id's and grades */

servedgr=indexc(prgoffered,grade);

run;

here is the output I get:

SID prgoffered grade servedgr

21185582 7 2 2
21609676 78 7 1
34645086 78 8 2
35962794 7 8 2
36489197 7 8 2
36784385 78 8 2
36982055 78 8 2
37097179 78 8 2
37220566 78 8 2
37228346 7 8 2
37241067 7 8 2
37398721 7 8 2
Olivier
Pyrite | Level 9
You problems seems to lie in unwanted spaces that the INDEXC functions takes into account.
Adding COMPRESS functions should resolve your problem and return to normal.

data elem;
INPUT SID $ prgoffered $ grade $ ;
servedgr=indexc(COMPRESS(prgoffered),COMPRESS(grade));
CARDS ;
21185582 7 2
21609676 78 7
34645086 78 8
35962794 7 8
36489197 7 8
36784385 78 8
36982055 78 8
37097179 78 8
37220566 78 8
37228346 7 8
37241067 7 8
37398721 7 8
;
run;
deleted_user
Not applicable
O my gosh!! That was it!! Thank you so much Olivier.

Ilona

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