Quartz | Level 8

## FCMP: Multiple arrays simplification

Hi,

I'd like to simplify my function by using only 1 array name so I can avoid to use an 'if then' statement at the end:

``````/*Get Tarif Paramaters*/
proc fcmp outlib=mydata.functions.func;
function Get_Parameter(Product \$, Sex,Smoker,Type_Tariff); /* Sex = 1 or 2, Smoker =1 or 2 , Type_Tariff= 1 or 2 */
if Product="A" then
do;
array f[2,2,2] (4 3,
2 1,
8 7,
6 5);
end;

else if Product="B" then
do;
array q[2,2,2] (-4 -3,
-2 -1,
-8 -7,
-6 -5);
end;

if Product="A" then
return ( f[Sex,Smoker,Type_Tariff]);
else if Product="B" then
return ( q[Sex,Smoker,Type_Tariff]);
endsub;

Data paramater;
do product= "A" ,"B";
do Sex=1 to 2;
do Smoker=1 to 2;
do Type_Tariff=1 to 2;
a=Get_Parameter(product,Sex,Smoker,Type_Tariff);
output;
end;
end;
end;
end;
run;``````

This is what I want but it doesn't work because I can't have the same array name for 2 different vectors.

``````/*Get Tarif Paramaters*/
proc fcmp outlib=mydata.functions.func;
function Get_Parameter(Product \$, Sex,Smoker,Type_Tariff); /* Sex = 1 or 2, Smoker =1 or 2 , Type_Tariff= 1 or 2 */
if Product="A" then
do;
array f[2,2,2] (4 3,
2 1,
8 7,
6 5);
end;

else if Product="B" then
do;
array f[2,2,2] (-4 -3,
-2 -1,
-8 -7,
-6 -5);
end;

return ( f[Sex,Smoker,Type_Tariff]);
endsub;

Data paramater;
do product= "A" ,"B";
do Sex=1 to 2;
do Smoker=1 to 2;
do Type_Tariff=1 to 2;
a=Get_Parameter(product,Sex,Smoker,Type_Tariff);
output;
end;
end;
end;
end;
run;``````

thank u for ur help

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: FCMP: Multiple arrays simplification

Why put data into a function?
Why use a function at all?

``````data lookup (index=(multi=(product sex smoker Type_Tariff)));
do product= "A" ,"B";
do Sex=1 to 2;
do Smoker=1 to 2;
do Type_Tariff=1 to 2;
input a @@;
output;
end;
end;
end;
end;
cards;
4  3     2  1    8  7    6  5
-4 -3    -2 -1   -8 -7   -6 -5
;;;;

data parameter;
do product= "A" ,"B";
do Sex=1 to 2;
do Smoker=1 to 2;
do Type_Tariff=1 to 2;
set lookup key=multi/unique ;
output;
end;
end;
end;
end;
stop;
run;

proc compare data=lookup compare=parameter;
run;
``````
7 REPLIES 7
Super User

## Re: FCMP: Multiple arrays simplification

Just add another dimension to your array and convert the A and B into integers.

Quartz | Level 8

## Re: FCMP: Multiple arrays simplification

I thought about this solution but it's quite inconvenient when u have a lot of different product. I only use 2 products in my example to simply my demand. Moreover, I need to perform some calculations on the array for some of the products.

Thx anyway for ur help

Super User

## Re: FCMP: Multiple arrays simplification

I thought about this solution but it's quite inconvenient when u have a lot of different product. I only use 2 products in my example to simply my demand. Moreover, I need to perform some calculations on the array for some of the products.

Thx anyway for ur help

Is there any reason why you are trying to use a function?

Why are you storing the data into a program to begin with?

Why not store the data into a dataset?

Then you could just use a index and set statement to retrieve the values.

Quartz | Level 8

## Re: FCMP: Multiple arrays simplification

Because I don't know where to begin with a datastep.

I know I can use a Proc Sql and merge with the dataset containing the values but I'm not a huge fan of this methodology.

Thx 🙂

Lapis Lazuli | Level 10

## Re: FCMP: Multiple arrays simplification

Since your array values differ by sign (+ or -) only, you can use a single array, f, defined once at the top (not inside if-then-do) and have:

``````if Product="A" then return ( f[Sex,Smoker,Type_Tariff]);
else if Product="B" then return ( -f[Sex,Smoker,Type_Tariff]);``````

Would that help?

Quartz | Level 8

## Re: FCMP: Multiple arrays simplification

Unfortunately not, I can have different values in the array so I could not take the negative. ^^

Thx for ur help though. 🙂

Super User

## Re: FCMP: Multiple arrays simplification

Why put data into a function?
Why use a function at all?

``````data lookup (index=(multi=(product sex smoker Type_Tariff)));
do product= "A" ,"B";
do Sex=1 to 2;
do Smoker=1 to 2;
do Type_Tariff=1 to 2;
input a @@;
output;
end;
end;
end;
end;
cards;
4  3     2  1    8  7    6  5
-4 -3    -2 -1   -8 -7   -6 -5
;;;;

data parameter;
do product= "A" ,"B";
do Sex=1 to 2;
do Smoker=1 to 2;
do Type_Tariff=1 to 2;
set lookup key=multi/unique ;
output;
end;
end;
end;
end;
stop;
run;

proc compare data=lookup compare=parameter;
run;
``````
Discussion stats
• 7 replies
• 770 views
• 3 likes
• 3 in conversation