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
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;
Just add another dimension to your array and convert the A and B into integers.
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
@adil256 wrote:
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.
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.
Could u tell me more about your solution with an index?
Thx 🙂
Hi adil256,
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?
Hi @LeonidBatkhan,
Unfortunately not, I can have different values in the array so I could not take the negative. ^^
Thx for ur help though. 🙂
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.