## Help with arrays, index/find, if and do statements

Occasional Contributor
Posts: 6

# Help with arrays, index/find, if and do statements

Hey all, I'm very new to SAS and have tried to find the answers to all this online but am still hitting roadblocks.

Here is the situation:

- I have a set of data where 15 of the variables are diagnoses (for a hospital) named dX1-dX15.

- for the 500 patients I'm working with, different codes have been assigned to them under these 15 variables

- I need to create a new variable "Prex" where every time one of the codes is present for a row, Prex=1 (and if not Prex=0)

- the codes I need are anything starting with 72, 73, 74 or with 650-669 or V27..

so the codes could be 72, 721, 7310, 74101, 658, 660445, V2791 etc.. (they all differ in length, but the key is that they start with any of           these options)

To this point, I have come up with:

data work.nist1;

set WORK.NIS_2002_10PCT_SAMPLE_A (obs=500);

keep key Prex dx1-dx15;

Array Diagn (15) dx1-dx15;

do i = 1 to 15;

if Diagn(i)=:'V270' or index(Diagn(i),'72') or index(Diagn(i),'73') or index(Diagn(i),'74')

then Prex=1;

end;

run;

proc print data=work.nist1;

id Prex;

run;

The problems I'm running into are:

- short of writing out an if statement for '650' '651' '652'... I have no idea how to index(or find) for the RANGE of codes starting with 650-669 (I thought maybe I could make a macro but that hasn't been working)

- I only need data that START with these numbers and yet data like '10973' are also showing up with Prex=1, so I'm unsure how to specify just the starting characters.

- I've tried the "find(Diagn(i),'72',startpos)" but was unsuccessful or maybe did it wrong.

- Also for character data you're supposed to be able to write " Where var Like 'N%' " and have it give you everything starting with N, regardless of what comes after. I figured out that for an IF statement, I can do "If var=:" as an equivalent to the like statement but it doesn't work to do

" If Diagn(i)=:'V27%' " - is there another symbol to use or what would be the best way to specify that the code #'s are at the start?

- Also originally I had an "Else Prex=0" statement before the End statement, but that was turning all of Prex into 0's (not sure why)

Hopefully this has made some sense and I'm really hoping that I'm not too far off from a solution.

Any help is massively appreciated.

Cheers,

Nick

Super User
Posts: 6,785

## Re: Help with arrays, index/find, if and do statements

Nick

Here are just a couple of pieces of the puzzle.

You've seen how INDEX locates the string anywhere within, not just at the beginning.  You've also used =: to examine the beginning of a string.  You can similarly use =: '72'.  And you can combine those using the IN operator:

if diag{i} in: ('V270', '72', '73', '74') then prex=1;

It doesn't matter that the arguments are of different length ... in: figures it out properly.

For character ranges, you can use something along these lines:

if ('650' <= diag{i} < '670') then ...

That might not get you all the way there, but it's a step or two further.

Good luck.

Occasional Contributor
Posts: 6

## Re: Help with arrays, index/find, if and do statements

Thanks a ton!

Occasional Contributor
Posts: 17

## Re: Help with arrays, index/find, if and do statements

The following code may get you started:

data have;

input code \$;

cards;

72

721

00721

653

667

vb665

;

data _null_;

length var \$ 5000;

do i=650 to 669;

var=catx('|', var,i);

end;

var=catx('|',var,72,73,74,'V27');

call symputx('var',var);

run;

data want;

set have;

prex = prxmatch("/^(&var)/", code)>0;

run;

Haikuo

Discussion stats
• 3 replies
• 225 views
• 3 likes
• 3 in conversation