I'm trying to make an array, here's my coding:
options nodate nonumber;
proc import out = project2
datafile= "\\Client\H$\Desktop\project2.xlsx"
dbms= XLSX replace;
getnames=yes;
run;
proc format;
value treat_no
1 = 'BYPASS'
2 = 'KIDNEYTRANSPLANT'
3 = 'PACEMAKER'
4 = 'ORALMEDS';
proc sort data=project2;
by id treat_no;
run;
proc print data=project2;
run;
data temp1;
array treatment[4] treat1-treat4;
retain treat1-treat4;
set project2;
by id;
if first.id then do i= 1 to 4;
treatment[i]=.;
end;
treatment[treat_no] = treat_no;
if last.id then output;
format treat1-treat4 treat_nof.;
keep id treat1-treat4;
run;
proc print data=temp1;
var id treat1-treat4;
run;
When I put this into SAS it comes back with the following errors:
ERROR: Variable treatment has been defined as both character and numeric.
ERROR 48-59: The format TREAT_NOF was not found or could not be loaded.
ERROR: Variable TREAT1 not found. (following the 'var treat1-treat4' statement).
I'm wondering if someone can kindly advise me on how to fix these errors?
Thanks!
ERROR: Variable treatment has been defined as both character and numeric.
array treatment[4] treat1-treat4;
-->
array _treatment[4] treat1-treat4;
also change your array name into _treatment in the following code.
ERROR 48-59: The format TREAT_NOF was not found or could not be loaded.
Your format name is treat_no NOT treat_nof
Cynthia,
I'm attempting to create the TREAT1-TREAT4.
Thanks!
By any chance, does your data set PROJECT2 contain a character variable named TREATMENT? That would conflict with an array called TREATMENT, containing numeric elements.
I wish it did! That would be a quick, logical fix. It contains a variable named treat_no, but not treatment.
What values do you want to see in treat1-treat4?
First up: the format issue - you defined a format name "TREAT_NO" but are trying to apply a format name "TREAT_NOF".
The answer to your headline question: How to define an array as numeric or character: When defining the array, after declaring the number of elements, the inclusion/exclustion of $ will determine if the array is character or numeric. Naturally a character array should only reference character variables, and a numeric array numeric variables. So:
array TestNums[4] Num1-Num4; /* Numeric array */
array TestChars[4] $ Char1-Char4; /* Character aray */
When the array is declared, if the "associated variables" do not yet exist in the PDV, the array statement will create them. If you do not include any "associated variables", then a sequence of variables with a prefix of the array name will be created. So:
array Dummy[10]; /* Will create numeric varables Dummy1, Dummy2, etc, up to Dummy 10 so that each array element can reference the relevant variable */
Your errors would be easier to diagnose if we knew the structure of the source data set WORK.PROJECT2.
I would suggest:
- place the ARRAY & RETAIN statements AFTER the SET statements, so that the array references existing variables, rather than creating them.
- if still failing, please post a PROC CONTENTS of the WORK.PROJECT2 data set so we are aware of all variables in the source data.
@boodaloo1 - do you require any further assistance with this question? If not, if any of the responses were useful, can you please select one as the solution to your question? Thanks.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.