DATA Step, Macro, Functions and more

How do I define an array as numeric or character?

Reply
Occasional Contributor
Posts: 6

How do I define an array as numeric or character?

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!

Super User
Posts: 9,671

Re: How do I define an array as numeric or character?

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

 

 

 

SAS Super FREQ
Posts: 8,740

Re: How do I define an array as numeric or character?

I see that you use a format of TREAT_NOF, but that is not what you have in your PROC FORMAT statement. It also looks like TREAT1-TREAT4 are coming from your work.project2 data? Or are you creating them. If they are character variables in work.project2, then you cannot format them with a numeric format.

Just curious.

cynthia
Occasional Contributor
Posts: 6

Re: How do I define an array as numeric or character?

Cynthia,

 

I'm attempting to create the TREAT1-TREAT4. 

 

Thanks!

Super User
Posts: 5,077

Re: How do I define an array as numeric or character?

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.

Occasional Contributor
Posts: 6

Re: How do I define an array as numeric or character?

I wish it did! That would be a quick, logical fix. It contains a variable named treat_no, but not treatment.

Super User
Posts: 17,775

Re: How do I define an array as numeric or character?

What values do you want to see in treat1-treat4?

Moderator
Posts: 238

Re: How do I define an array as numeric or character?

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. 

Ask a Question
Discussion stats
  • 7 replies
  • 797 views
  • 0 likes
  • 6 in conversation