BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
adil256
Quartz | Level 8

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
Tom
Super User Tom
Super User

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;

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

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

 

adil256
Quartz | Level 8

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

Tom
Super User Tom
Super User

@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.

adil256
Quartz | Level 8

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 🙂

LeonidBatkhan
Lapis Lazuli | Level 10

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?

adil256
Quartz | Level 8

Hi  @LeonidBatkhan,

 

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

 

Thx for ur help though. 🙂

 

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 764 views
  • 3 likes
  • 3 in conversation