Macro : Repeative Case statement into a Do Loop

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

Macro : Repeative Case statement into a Do Loop

Hello,

 

I am trying to make this script store all values of i, not just the last one. At the moment the code keeps just last number (given by macro) from the loop. Any suggestions? Thanks

 

%MACRO M(N);

DATA _NULL_;

%DO i=1 %TO &N.;

PROC SQL;

CREATE TABLE G AS

SELECT VAR1,

VAR2,

CASE WHEN VAR2 <= (1-(&i-1)/10) * MAX(VAR2) AND VAR2> (1-&i/10) * MAX(VAR2) THEN "Q&i" END AS FLAG

FROM B;

QUIT;

%END;

%MEND M;

%M(10)


Accepted Solutions
Solution
‎07-22-2016 07:56 AM
Esteemed Advisor
Posts: 6,647

Re: Macro : Repeative Case statement into a Do Loop

%let maxpart=10;

proc sql noprint;
select max(var2) into :maxvar2 from b;
quit;

data want;
set b;
length flag $3;
do i = 1 to &maxpart;
  if (1 - i / &maxpart) * &maxvar2 < var2 <= (1 - (i - 1) / &maxpart) * &maxvar2
  then flag = 'Q' !! put(i,z2.);
end;
drop i;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Macro : Repeative Case statement into a Do Loop

Could you post example test data in the form of a datastep, and what your output needs to look like.  The reason is that your code doesn't make sense.  Let me go through it;

Firstly, there is almost never a need to deafult to macro language, your just overcomplicating the problem.  Secondly that data _null_ step does nothing.  The SQL procedure is repeated i times, each time it creates a new table, hence only the last iteration will be available.  Thiis should be pretty straight forward in a datastep, however will need to see the input data and what the output should look like.

Contributor
Posts: 44

Re: Macro : Repeative Case statement into a Do Loop

I need to flag data based on value set in macro. Lets say I want a decile distribution, then I set 10 into macro, if quartiles needed I set 4...macro is not a problem there I believe..

Solution
‎07-22-2016 07:56 AM
Esteemed Advisor
Posts: 6,647

Re: Macro : Repeative Case statement into a Do Loop

%let maxpart=10;

proc sql noprint;
select max(var2) into :maxvar2 from b;
quit;

data want;
set b;
length flag $3;
do i = 1 to &maxpart;
  if (1 - i / &maxpart) * &maxvar2 < var2 <= (1 - (i - 1) / &maxpart) * &maxvar2
  then flag = 'Q' !! put(i,z2.);
end;
drop i;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 44

Re: Macro : Repeative Case statement into a Do Loop

Thank you!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 337 views
  • 1 like
  • 3 in conversation