Obsidian | Level 7

## Lookup using array

input id ae1-ae10;
cards;
101 1 2 2 3 4 1 1 5 4 1
102 1 2 2 4 3 1 2 1 1 1
103 1 2 4 1 3 3 1 1 2 2
104 2 1 3 1 3 2 2 1 5 5
105 1 1 2 4 5 1 3 5 4 1
106 1 2 2 1 1 3 3 5 5 1
107 3 2 2 1 1 2 2 3 3 3
108 5 2 1 1 1 1 1 2 2 5
;

Conditions :

1 = mild

2 = moderate

3 = severe

4 = life_threat

5 = death

I attempted the code below, but I'm encountering errors in the log.

array aem{10}  ae1 - ae10;

array sev{5} _temporary_ ('mild','moderate','severe','life_threatening','death');

do i = 1 to 10;
do j = 1 to 5;
if aem[i] = sev[j] then aem[i] = sev[j];

end;
end;
run;

log
```627  data task_complete;
629     array aem{10}  ae1 - ae10;
630
631     array sev{5} _temporary_ ('mild','moderate','severe','life_threatening','death');
ERROR: Attempt to initialize element of numeric array sev with character constant 'mild'.
ERROR: Attempt to initialize element of numeric array sev with character constant 'moderate'.
ERROR: Attempt to initialize element of numeric array sev with character constant 'severe'.
ERROR: Attempt to initialize element of numeric array sev with character constant 'life_threatening'.
ERROR: Attempt to initialize element of numeric array sev with character constant 'death'.
632
633     do i = 1 to 10;
634     do j = 1 to 5;
635     if aem[i] = sev[j] then aem[i] = sev[j];
636
637      end;
638       end;
639     run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TASK_COMPLETE may be incomplete.  When this step was stopped there were 0
observations and 13 variables.
WARNING: Data set WORK.TASK_COMPLETE was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time           0.04 seconds
cpu time            0.00 seconds```

7 REPLIES 7
Diamond | Level 26

## Re: array

Please show us the ENTIRE log for this code. Copy the log as text and paste it into the window that appears when you click on the </> icon.

Never tell us you get errors in the log, and then not show us the log.

--
Paige Miller
Obsidian | Level 7

## Re: array

Okay
thank u
Diamond | Level 26

## Re: array

To fix the error in your log, use a dollar sign to indicate this array is character, not numeric

``````   array sev{5} \$ _temporary_ ('mild','moderate','severe','life_threatening','death');
``````

This still results in a useless program, since aem[i] is numeric and it cannot be tested to be equal (or not) to sev[j] which is character.

It would really help, not just in this thread, but in all future questions that you ask, if you EXPLAIN what you are trying to do, rather than simply ask for help fixing the code. Do not just dump code onto the screen without explanation. If you EXPLAIN what you are trying to do, we can often get to the root of the problem and fix the issue much more easily. If you do not EXPLAIN what you are trying to do, then we have to guess what you are trying to do, and that's rarely a good thing. So please tell us what is this code trying to do?

My guess is that you want values of 1 to appear as the word mild, and so on. That's a guess. Am I right? Can you please EXPLAIN what this code is supposed to do?

--
Paige Miller
Super User

## Re: array

Array aem is numeric, array sev is character, so you cannot store a sev value in aem.

Super User

## Re: array

If you wan the temporary array to contain character variables and not the default numeric variables you need to TELL that to the data step compiler by including the LENGTH specification in the ARRAY statement.

`` array sev{5} \$20 _temporary_ ('mild','moderate','severe','life_threatening','death');``

But you also cannot assign a value of 'mild' the any of the variables in the AEM array since you have already defined all of those variables are numeric in the TASK dataset.

But why do you need to create new character variables?  Why not just attach a FORMAT to your existing numeric variables?

``````
proc format;
value condition
1 = 'mild'
2 = 'moderate'
3 = 'severe'
4 = 'life_threatening'
5 = 'death'
;
run;

input id ae1-ae10;
format ae1-ae10 condition. ;
cards;
101 1 2 2 3 4 1 1 5 4 1
102 1 2 2 4 3 1 2 1 1 1
103 1 2 4 1 3 3 1 1 2 2
104 2 1 3 1 3 2 2 1 5 5
105 1 1 2 4 5 1 3 5 4 1
106 1 2 2 1 1 3 3 5 5 1
107 3 2 2 1 1 2 2 3 3 3
108 5 2 1 1 1 1 1 2 2 5
;

proc print;
run;``````
```Obs  id ae1      ae2      ae3              ae4              ae5              ae6      ae7      ae8      ae9              ae10

1  101 mild     moderate moderate         severe           life_threatening mild     mild     death    life_threatening mild
2  102 mild     moderate moderate         life_threatening severe           mild     moderate mild     mild             mild
3  103 mild     moderate life_threatening mild             severe           severe   mild     mild     moderate         moderate
4  104 moderate mild     severe           mild             severe           moderate moderate mild     death            death
5  105 mild     mild     moderate         life_threatening death            mild     severe   death    life_threatening mild
6  106 mild     moderate moderate         mild             mild             severe   severe   death    death            mild
7  107 severe   moderate moderate         mild             mild             moderate moderate severe   severe           severe
8  108 death    moderate mild             mild             mild             mild     mild     moderate moderate         death```

Super User

## Re: array

If you want to DISPLAY text associated with a numeric value then the SAS approach is to create a custom FORMAT.

Is this what you really want? Look at the proc print output.

```proc format;
value AE
1 = 'mild'
2 = 'moderate'
3 = 'severe'
4 = 'life_threat'
5 = 'death'
;
run;

input id ae1-ae10;
cards;
101 1 2 2 3 4 1 1 5 4 1
102 1 2 2 4 3 1 2 1 1 1
103 1 2 4 1 3 3 1 1 2 2
104 2 1 3 1 3 2 2 1 5 5
105 1 1 2 4 5 1 3 5 4 1
106 1 2 2 1 1 3 3 5 5 1
107 3 2 2 1 1 2 2 3 3 3
108 5 2 1 1 1 1 1 2 2 5
;

format ae1-ae10 ae.;
run;```

Formats are extremely powerful tool in SAS. Example above shows using one format to display the same text for multiple variables. No reassignment of variables needed. No new variables needed. The ugly logic some times involved, such as ranges of numeric values is moved out of other code.

Also you can create custom groupings of your values

```proc format;
value AE_2level
1,2 = 'mild to moderate'
3,4,5 = 'severe to death'
;
run;
format ae1-ae10 ae_2level.;
run;```

The groups created by formats will be honored by analysis reporting and generally graphing procedures (some exceptions with custom date, time and datetime formats). Example:

```proc freq data=task;
tables ae1 - ae10;
format ae1-ae10 ae_2level.;
run;```

The limitation on formats is that they map a single variable to the desired values. If you have logic that needs two or more variables to build the new "display" value then a format won't work.

SAS Super FREQ

## Re: array

Hi:
And, as a final footnote to the discussion of Table Lookups, here's a very nice (although older) user group paper about different lookup techniques and the pros/cons of each:
https://support.sas.com/resources/papers/proceedings/pdfs/sgf2008/095-2008.pdf

Cynthia
Discussion stats
• 7 replies
• 275 views
• 1 like
• 6 in conversation