DATA Step, Macro, Functions and more

question about array

Accepted Solution Solved
Reply
Super User
Super User
Posts: 7,970
Accepted Solution

question about array

This came up in another question, and would like to get some guidance, otherwise I might add this as a feature request.  The question was, having a set of non-standard variables names, how to move these to an enumerated list, like sq_1, sq_2 etc.

There are a number of ways to do it, however I was wondering why the below does not work - it states that sq_1... is not found:

data have;
  vara=1; varxyz=2; abc=4;
run;

data want (keep=sq_:);
  set have;
  array sq_{*} vara--abc;
run;

However the following, where I also create an array, does get retained:

data have;
  vara=1; varxyz=2; abc=4;
run;

data want (keep=sq_:);
  set have;
  array aq_{*} vara--abc;
  array sq_{3} 8.;
  do i=1 to 3;
    sq_{i}=aq_{i};
  end;
run;

It seems like setting an array with a list of variables defaults it to _temporary_, which I suppose makes sense, but there are also times when you may want to keep those variables, so maybe an option _nottemporary_.  But I couldn't find anything like that.  Seems a bit of a faff to have to code around it.

 

 

 

 

 


Accepted Solutions
Solution
‎01-20-2016 09:45 AM
Trusted Advisor
Posts: 1,117

Re: question about array

Maybe, back then when the array feature was designed and memory was scarce, the developers were hesitant to provide an option which would create redundancy by duplicating variables (although possible in principle). So, they came up with the pointer idea.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,117

Re: question about array

Please see my explanation in the original thread.

Super User
Super User
Posts: 7,970

Re: question about array

Posted in reply to FreelanceReinhard

Hi,

 

Yes, lets carry it on here rather than jumping someone else's post.  I do understand that that is the default action, if the variables exist then a pointer to them is created rather than a variable.  Even so, personally I would like to see an option that these actually be created, it is something that comes up a fair bit - going from list of variables to array processing, even if only to simplify coding.

Solution
‎01-20-2016 09:45 AM
Trusted Advisor
Posts: 1,117

Re: question about array

Maybe, back then when the array feature was designed and memory was scarce, the developers were hesitant to provide an option which would create redundancy by duplicating variables (although possible in principle). So, they came up with the pointer idea.

Super User
Super User
Posts: 7,970

Re: question about array

Posted in reply to FreelanceReinhard

Thanks, suppose I will have to stick with the faffing about then Smiley Surprised)

Respected Advisor
Posts: 3,799

Re: question about array

You keep saying that which makes me think you still don't understand data step arrays and what.. 

 

array sq_[*] list-of-variables-names;

 

Does and perhaps more importantly does not do.

 

Please read the links from my previous reply.

Super User
Posts: 5,511

Re: question about array

Just for the record, some renames of lists are supported:

 

rename a1-a3 = sq_1 - sq_3;

 

But some are not:

 

rename var1--abc = sq_1 - sq_3;

 

That may be another way to look at the feature you would like to have.

 

Super User
Super User
Posts: 7,970

Re: question about array

Posted in reply to Astounding

Thanks.  Yes its another way to look at it.  I presume the second doesn't work as it doesn't know how many in the first list of variables there are.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 277 views
  • 1 like
  • 4 in conversation