The SAS Output Delivery System and reporting techniques

need help with INDEX function

Reply
N/A
Posts: 0

need help with INDEX function

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 ????
Contributor
Posts: 49

Re: need help with INDEX function

Posted in reply to deleted_user
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
N/A
Posts: 0

Re: need help with INDEX function

Posted in reply to deleted_user
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
Super Contributor
Posts: 260

Re: need help with INDEX function

Posted in reply to deleted_user
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;
N/A
Posts: 0

Re: need help with INDEX function

Posted in reply to deleted_user
O my gosh!! That was it!! Thank you so much Olivier.

Ilona
Ask a Question
Discussion stats
  • 4 replies
  • 137 views
  • 0 likes
  • 3 in conversation