Fluorite | Level 6

## Using array to create a new variable

Hello friends,

I have the following data set. I'm trying to create a variable 'nccs', which is first number between 11-45 among the variables DXCCS1-DXCCS5. That way, 'nccs' would be similar to variable 'Result'.

data data;
input ID DCCS1 DXCCS2 DXCCS3 DXCCS4 DXCCS5 Result;
datalines;
1 67 73 43 1 34 43
2 2 87 67 62 92 .
3 9 32 . 5 2 32
4 34 6 90 93 88 34
5 5 53 84 68 4 .
6 55 76 67 56 29 .
7 12 82 . . 11 12
8 10 49 76 67 99 .
9 78 24 56 93 11 24
10 3 1 2 82 99 .
run;

I used the following code:

data data_1;
set data;
array DXCCS DXCCS1-DXCCS5;
do h=1 to dim(DXCCS);
if DXCCS(h) ne . then do;
if nccs_CCS = . and (DXCCS(h)>11 and DXCCS(h)<45) then nccs = DXCCS(h);
end;
end;
run;

Can someone help me fix it.

Thank you and have a wonderful weekend!

Rube

2 REPLIES 2
Super User

## Re: Using array to create a new variable

``` 88         data data_1;
89         set data;
90         array DXCCS DXCCS1-DXCCS5;
91         do h=1 to dim(DXCCS);
92         if DXCCS(h) ne . then do;
93         if nccs_CCS = . and (DXCCS(h)>11 and DXCCS(h)<45) then nccs = DXCCS(h);
94         end;
95         end;
96         run;

NOTE: Variable nccs_CCS is uninitialized.
```

The NOTE tells you that you try to use a variable that

• does not exist in the incoming dataset
• is never set to a value in the step

I guess that nccs_CCS is a typo and you wanted to use nccs.

## Re: Using array to create a new variable

Hello @sandrube,

You can use a DO-UNTIL loop:

``````/* Create sample data */

data have;
input ID DXCCS1-DXCCS5 Result;
datalines;
1 67 73 43 1 34 43
2 2 87 67 62 92 .
3 9 32 . 5 2 32
4 34 6 90 93 88 34
5 5 53 84 68 4 .
6 55 76 67 56 29 29
7 12 82 . . 11 12
8 10 49 76 67 99 .
9 78 24 56 93 11 24
10 3 1 2 82 99 .
;

/* Find first array value in ]11, 45[ */

data want(drop=h);
set have;
array DXCCS[*] DXCCS:;
do h=1 to dim(DXCCS) until(11<DXCCS(h)<45);
end;
if h<=dim(DXCCS) then nccs=DXCCS(h);
run;``````

Note that I've corrected your missing Result for ID 6 and the typo in "DCCS1".

Discussion stats
• 2 replies
• 1073 views
• 0 likes
• 3 in conversation