Help using Base SAS procedures

defining arrays using values from other fields

Accepted Solution Solved
Reply
Super Contributor
Posts: 259
Accepted Solution

defining arrays using values from other fields

In the table below I want to define an array basd on the start and end value (first two columns). The array for the first row is start 1 and end 5 so the array will go from 101% to 116% (dimension 5); the array for the second row will go from 102% to 108% (dimension 2) and so on.

 

can you create an array using the start and end variable names i.e.

 

array temp{*} price_start --price_end

 

 

startendprice_1price_2price_3price_4price_5price_6price_7price_8price_9price_10price_1112131415
15101%102%107%108%116%120%128%130%139%140%146%152%157%164%167%
23101%102%108%112%114%116%122%129%133%141%146%149%158%165%172%
510101%102%110%114%115%122%129%132%136%144%153%161%166%168%175%
1214101%102%108%117%127%135%144%150%155%155%157%160%162%166%170%
37101%102%107%114%118%126%130%132%136%138%138%147%152%158%162%
58101%102%111%111%120%127%129%131%137%140%149%155%162%164%173%
68101%102%110%119%126%132%138%140%147%150%151%152%159%160%164%

Accepted Solutions
Solution
‎04-10-2017 01:37 PM
Super User
Posts: 5,082

Re: defining arrays using values from other fields

In a word, no.  Any arrays that you define must have exactly the same dimensions, for every observation.

 

What you can do is define an array that includes all the percent variables, and then process just a subset.  For example:

 

array all {*} price_: ;

do i=start to end;

   * some logic that works with all{i};

end;

View solution in original post


All Replies
Solution
‎04-10-2017 01:37 PM
Super User
Posts: 5,082

Re: defining arrays using values from other fields

In a word, no.  Any arrays that you define must have exactly the same dimensions, for every observation.

 

What you can do is define an array that includes all the percent variables, and then process just a subset.  For example:

 

array all {*} price_: ;

do i=start to end;

   * some logic that works with all{i};

end;

Super Contributor
Posts: 259

Re: defining arrays using values from other fields

Thanks astounding. I think that could work but is there a way of referencing the field name of variables in the array you want to exclude I.e. If number corresponding to field name is less than start than skip ... that kind of thing
Super User
Posts: 5,082

Re: defining arrays using values from other fields

To some extent, that's what the posted code does.  The words START and END do not refer to the start and end of the array.  Rather, they refer to your DATA step variables START and END.  When START=5 and END=8, this means you will process the 5th through 8th variables using the DO loop.

 

If you want something more complex, it is possible to get the name of the variable as the loop proceeds.  For example, START=5, END=12, and you want to skip the variable price_10 while otherwise processing price_5 through price_12:

 

do i=start to end;

   if upcase(vname(all{i})) ne 'PRICE_10' then ......

end;

 

I'm suspecting this is overkill and the original code is just what you are looking for.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 239 views
  • 0 likes
  • 2 in conversation