data task;
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.
627 data task_complete; 628 set task; 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
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.
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?
Array aem is numeric, array sev is character, so you cannot store a sev value in aem.
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;
data task;
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
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; data task; 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 ; proc print data=task; 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; proc print data=task; 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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.