Help using SAS array code

Reply
New Contributor MIR
New Contributor
Posts: 3

Help using SAS array code

I am transitioning SAS user from STATA and need some help with the array function. Help would be greatly appreciated !!

There are six columns of diagnosis codes for 20 patients (all fake data)

For each variable below, use the algorithm to write the logic to select and code the IDs correctly, then list the ids that will be in each new variable.

HBP

Binary variable (0=no codes for hypertension in any coding position for Dxn; 1=at least ONE of the codes in any of the coding

positions for Dxn) Dxn:401-405.

msepsis

Binary variable (0=no codes for sepsis in any coding position for Dxn; 1=at least one of the codes on any of the coding positions.

Dxn:038,020.0,790.7,117.9,112.81.

asepsis

Binary variable (0=does not meet criteria for severe sepsis, 1= meets criteria for severe sepsis defined as at least one code for

infection in any coding position for Dxn Dxn:001,023,033,034,095      


PLUS at least one code for organ dysfunction in any coding position for Dxn. Dxn: 518.81,518.82,518.85,458.0,785.5,785.5,584,580,570,572,286.2,286.6)

New Contributor MIR
New Contributor
Posts: 3

Re: Help using SAS array code

Here is the data:

IDdx1dx2dx3dx4dx5dx6
140138789.00274.9462536.8
2255.5346.90402308.0305.00707.81
340303302358038995.3
4455.6401726.32845.00388.30463
51274.0403244.9785.5405
6302.71095584708.0787.3487.1
7401845.00627.238723.1311.0
8001786.2458.0724.2020.0523.0
9473.9401729.1005.9250.0625.4
10841.9790.7617.0038780.79715.00
11785.5625.3034724.3053.9842.01
12309.1564.00402304.00405729.5
13460555.9401723.1719.42117.9
14625.9304.00787.02401844.9112.0
15402038786.2460112.81780.51
16078.1847.2790.7354.0402346.1
17780.8402785.1458.0278.0783.2
18351.0274.0095783.2286.2719.47
19724.4020.00729.5562.11787.91460
2095580286.2564.00117.9518.85
Grand Advisor
Posts: 17,313

Re: Help using SAS array code

Here's an example for your first one, you should be able to extrapolate from it.

data want;

set have;

array dx(6) dx1-dx6;

hbp=0;

do i=1 to dim(dx);

if dx(i) in (401, 402, 403, 404, 405) then hbp=1;

end;

run;

New Contributor MIR
New Contributor
Posts: 3

Re: Help using SAS array code

Thank you so much!

Do I have to run each diagnosis separate (array...to end, run)? Or is there a way to do it all at once....

EXAMPLE.

data want;

set have;

array dx(6) dx1-dx6;

hbp=0;

do i=1 to dim(dx);

if dx:smileyinfo: in (401, 402, 403, 404, 405) then hbp=1;

mspesis=0;

do i=1 to dim(dx);

if dx:smileyinfo: in (038,020.0,790.7,117.9,112.81) then mspesis=1;

etc.

end;

run;

Grand Advisor
Posts: 17,313

Re: Help using SAS array code

You can put all your if statements in the do loop.

Make sure to initialize the variablea to 0 before the loop.

Super User
Super User
Posts: 6,315

Re: Help using SAS array code

It is a little easier if you store you DX codes as character.  Most usage I have seen store them without the periods.

data have ;

  length id 8 dx1-dx6 $6 ;

  input id dx1-dx6 ;

cards;

1  401 038   78900 2749  462 5368

2  2555 34690 402   3080  30500 70781

3  403 033   023   580 038   9953

4  4556 401   72632 84500 38830 463

5  001 2740  403   2449 7855  405

6  30271 095 584   7080  7873 4871

7  401 84500 6272  038   7231 3110

8  001 7862  4580  7242 0200  5230

9  4739 401   7291  0059 2500  6254

10 8419  7907 6170  038   78079 71500

11 7855  6253 034   7243  0539 84201

12 3091  56400 402 30400 405   7295

13 460   5559 401   7231  71942 1179

14 6259  30400 78702 401   8449 1120

15 402   038 7862  460   11281 78051

16 781   8472 7907  3540  402 3461

17 7808  402 7851  4580  2780 7832

18 3510  2740 095   7832  2862 71947

19 7244  02000 7295 56211 78791 460

20 095   580 2862  56400 1179  51885

;;;;

data want ;

  set have ;

  array codes (4) $200 _temporary_

('401 402 403 404 405'

,'038 200 7907 1179 11281'

,'001 023 033 034 095'

,'51881 51882 51885 458 7855 7855 584 580 570 572 2862 2866'

) ;

  array flags (4) hbp msepsis asepsis organ_dys ;

  array dx (6);

  do i=1 to dim(flags) ;

    do j=1 to dim(dx) until (flags(i)=1);

      flags(i)=0^=indexw(codes(i),dx(j));

    end;

  end;

  drop i j ;

run;

data expected;

input id hbp msepsis asepsis organ_dys ;

cards;

  1 1  1  0  0

  2 1  0  0  0

  3 1  1  1  1

  4 1  0  0  0

  5 1  0  1  1

  6 0  0  1  1

  7 1  1  0  0

  8 0  0  1  0

  9 1  0  0  0

10 0  1  0  0

11 0  0  1  1

12 1  0  0  0

13 1  1  0  0

14 1  0  0  0

15 1  1  0  0

16 1  1  0  0

17 1  0  0  0

18 0  0  1  1

19 0  0  0  0

20 0  1  1  1

run;

proc compare data=expected compare=want ;

  id id;

run;

Ask a Question
Discussion stats
  • 5 replies
  • 393 views
  • 1 like
  • 3 in conversation