BookmarkSubscribeRSS Feed
justforgetit
Calcite | Level 5
Is it possible to create a array with length after calculations in data steps or in macro?
for example, i calculate the length of array required is array_length:
data;
a='asdfgh';
b='axmnbkl';
n=length(a);
m=length(b);
array_length=2*(n+m)+1;
array V(array_length);
run;

of course the above example will return errors, but it illustrate what i want to do.
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
The parameter you are attempting to set (subscript -- within the parentheses) has no bearing on the SAS variable value length at all. Re-visit the DOC.

Suggest you start over and explain what it is you want to accomplish with the SAS system and why.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search arguments, this topic / post:

array statement site:sas.com

array introduction site:sas.com
Elle
Quartz | Level 8

Hi, I was just doing something similar, extracting a word of a string into a different variable and some words got truncated because of the default length of an array is set to 8.

The way to go around it is to put a really big length for the array.

My code is as follows:

 

data work.smallranges_desc1;
set work.smallranges;
array word {&maxlength.} $100.;
do i=1 to &maxlength.;
word[i]=scan(range,i);
end;
run;

jimbarbour
Meteorite | Level 14

@Elle,

 

I think you may be confusing the number of occurrences of the array with the length.

 


@Elle wrote:

array word {&maxlength.} $100.;


What you have in {braces} is not the length.  $100 is the length of each variable in the array.  "&maxlength" is not a length at all but rather is the number of occurrences in the array.

 

For example, let's say I have some variables in a SAS dataset.  Each variable is character.  The names of the variables are Animal1 - Animal6.  Let's say each variable is $32.

 

I would declare the array as follows:

ARRAY Animals {6} $32 Animal1 - Animal6;

 

The number "6" is not a length but the number of occurrences.  $32 is the length of each occurrence.

 

For example, say I have the following SAS code:

 

DATA	Have;
	INPUT	Animal1 : $32.
			Animal2 : $32.
			Animal3 : $32.	
			Animal4 : $32.
			Animal5 : $32.	
			Animal6 : $32.
			;
DATALINES;
bird cat aardvark tarantula tapir elephant
dog owl mouse hippopotamus lamb lemur 
panda piranha parakeet pig peacock penquin
armadillo fringehead jellyfish alligator wallaby pademelon
;
RUN;

DATA	Want;
	DROP	_:;
	SET	Have;
	ARRAY	Animals {6}	$8	Animal1 - Animal6;
	DO	_i	=	1	TO	6;
		Animals{_i}	=	PROPCASE(Animals{_i});
	END;
RUN;

PROC	PRINT	DATA	=	Want;
RUN;

 

 

My array definition is 

ARRAY Animals {6} $8 Animal1 - Animal6;

Notice my results:  Some of the names are truncated.

jimbarbour_0-1628714042094.png

 

Now, change the array definition from $8 to $32:

ARRAY Animals {6} $32 Animal1 - Animal6;

In the results, the truncation is gone.

jimbarbour_1-1628714173724.png

 

In both of my definitions, the number of occurrences was {6}.  This did not change.  The length of each variable however did change.  It went from $8 to $32.  The length definition is what you need to change in order to fix truncation, not the number of occurrences.

  1. First definition (too short):  ARRAY Animals {6} $8 Animal1 - Animal6;

  2. Second definition (correct) ARRAY Animals {6} $32 Animal1 - Animal6;

 

Jim

Elle
Quartz | Level 8
Thanks for the lengthy reply. Personally, I am not confusing anything. My &maxlength. was 6 which resulted in 6 new variables in my case. I was having an issue with the default length of the 6 variables, which was truncating the strings I assigned to them. The way I read OP's question, it seemed he wanted to store different lengths in his array variables so I provided my solution. If OP was referring to something else, he's have to jump to some other answer.
Peter_C
Rhodochrosite | Level 12
hi justforgetit
the simple answer for you is NO

SAS data step arrays have a length that must be declared at compile-time and the kind of variables you demonstrate using, have no value at compile time.

Data step hash tables (also called associative arrays) define their memory at "run-time" and so these could be defined as you seem to seek. However hash tables are defined differently from your example.

good luck
peterC
Cynthia_sas
SAS Super FREQ
Hi:

This paper is a good introduction to Array processing:
http://www2.sas.com/proceedings/forum2007/273-2007.pdf

Normally, you do not need to worry about the length of an array, because in SAS, an array is not a physical data construct (as it is in some languages). An array in SAS is a convenient way to reference a group of separate variables as though they were stored in an array.

The variables, when they are stored internally in a SAS dataset are stored by their individual names. For the duration of a DATA step program, however, you can treat a group of variables as though they were members of an array for ease of processing and reference. So these are all valid DATA step array references:
[pre]
array lovelucy $ fred ethel lucy ricky;
array regsl regsale1 regsale2 regsale3 regsale4;
[/pre]

In the first array, LOVELUCY, the character variables FRED, ETHEL, LUCY and RICKY are being treated as array members LOVELUCY(1), LOVELUCY(2), LOVELUCY(3) and LOVELUCY(4) respectively. While in the second ARRAY statement, the REGSL array will be composed of the individual variables REGSALE1, REGSALE2, REGSALE3 and REGSALE4 and can be treated as array members using the array syntax: REGSL(1), REGSL(2), REGSL(3) and REGSL(4).

I could also declare an array to be composed of all my character variables or all my numeric variables (which I might want to do if I was testing for the presence or absence of missing values):
[pre]
array cv $ _character_;
array nv _numeric_;
[/pre]

In a DATA step program you could use a DATA step DO loop to iterate through an array in order to perform some kind of processing. For the first 2 ARRAY statements, the number of variables in the array is known, but in the second set of ARRAY statements, the number of variables is unknown. However, the DIM function would allow me to operate a DO loop in the DATA step from 1 to the DIM(...) of the array.

Consider this program below, which treats all the numeric variables in SASHELP.CLASS like array members (program and log output shown here).

cynthia
[pre]
529
530 data testit;
531 set sashelp.class(obs=5);
532 array myv _numeric_;
533 do i = 1 to dim(myv);
534 thisvar = vname(myv(i));
535 put _n_= Name= i= thisvar= ' value is: ' myv(i);
536 end;
537 run;

_N_=1 Name=Alfred i=1 thisvar=Age value is: 14
_N_=1 Name=Alfred i=2 thisvar=Height value is: 69
_N_=1 Name=Alfred i=3 thisvar=Weight value is: 112.5
_N_=2 Name=Alice i=1 thisvar=Age value is: 13
_N_=2 Name=Alice i=2 thisvar=Height value is: 56.5
_N_=2 Name=Alice i=3 thisvar=Weight value is: 84
_N_=3 Name=Barbara i=1 thisvar=Age value is: 13
_N_=3 Name=Barbara i=2 thisvar=Height value is: 65.3
_N_=3 Name=Barbara i=3 thisvar=Weight value is: 98
_N_=4 Name=Carol i=1 thisvar=Age value is: 14
_N_=4 Name=Carol i=2 thisvar=Height value is: 62.8
_N_=4 Name=Carol i=3 thisvar=Weight value is: 102.5
_N_=5 Name=Henry i=1 thisvar=Age value is: 14
_N_=5 Name=Henry i=2 thisvar=Height value is: 63.5
_N_=5 Name=Henry i=3 thisvar=Weight value is: 102.5
[/pre]

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 7816 views
  • 1 like
  • 6 in conversation