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

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

6 REPLIES 6
mazouz
Calcite | Level 5
integer
%let d1 = 0;
%let d2 = 1;
%let d3 = 2;
%let d4 = 5;
%let d5 = 6;
and
p&d1 ... is quantiles (integer)
pctlpts =&d1,&d2,&d3,&d4,&d5,&d6,&d7,&d8,&d9,&d10,&d11 pctlpre=p;
Kurt_Bremser
Super User

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;

?

mazouz
Calcite | Level 5
yes like this but I want to keep macro variables
Kurt_Bremser
Super User

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;
mazouz
Calcite | Level 5

Thank you, that work for me 🙂

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1320 views
  • 0 likes
  • 2 in conversation