Hello, this code work for me but is long, I want to optimize my code. by using loop or proc sql or ...
data A;
set B;
if p&d2/p&d1 > &seuil then
do;
seuil_pr_var=p&d2;
quantile_pr_var=&d2;
end;
else if p&d3/p&d2 > &seuil then
do;
seuil_pr_var=p&d3;
quantile_pr_var=&d3;
end;
else if p&d4/p&d3 > &seuil then
do;
seuil_pr_var=p&d4;
quantile_pr_var=&d4;
end;
else if p&d5/p&d4 > &seuil then
do;
seuil_pr_var=p&d5;
quantile_pr_var=&d5;
end;
if p&d5/p&d4 > &seuil then
do;
seuil_dr_var=p&d5;
quantile_dr_var=&d5;
end;
else if p&d4/p&d3 > &seuil then
do;
seuil_dr_var=p&d4;
quantile_dr_var=&d4;
end;
else if p&d3/p&d2 > &seuil then
do;
seuil_dr_var=p&d3;
quantile_dr_var=&d3;
end;
run;
Reduce the use of the macro variables to ARRAY statements:
data A;
set B;
array p {5} p&d1 p&d2 p&d3 p&d4 p&d5;
array d {5} _temporary_ (&d1 &d2 &d3 &d4 &d5);
do i = 2 to 5;
if p{i} / p{i-1} > &seuil
then do;
seuil_pr_var = p{i};
quantile_pr_var = d{i};
leave;
end;
end;
do i = 5 to 3 by -1;
if p{i} / p{i-1} > &seuil
then do;
seuil_dr_var = p{i};
quantile_dr_var = d{i};
leave;
end;
end;
run;
What do your macro variables d1 to d5 contain?
So the start of your code is in fact this:
data A;
set B;
if p1/p0 > &seuil then
do;
seuil_pr_var=p1;
quantile_pr_var=1;
end;
else if p2/p1 > &seuil then
do;
seuil_pr_var=p2;
quantile_pr_var=2;
end;
?
Reduce the use of the macro variables to ARRAY statements:
data A;
set B;
array p {5} p&d1 p&d2 p&d3 p&d4 p&d5;
array d {5} _temporary_ (&d1 &d2 &d3 &d4 &d5);
do i = 2 to 5;
if p{i} / p{i-1} > &seuil
then do;
seuil_pr_var = p{i};
quantile_pr_var = d{i};
leave;
end;
end;
do i = 5 to 3 by -1;
if p{i} / p{i-1} > &seuil
then do;
seuil_dr_var = p{i};
quantile_dr_var = d{i};
leave;
end;
end;
run;
Thank you, that work for me 🙂
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.