DATA Step, Macro, Functions and more

Repeat codes following a condition

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 110
Accepted Solution

Repeat codes following a condition

Hello,

I have a variable X with 10 categories (1, 2, 3,...10). The data set has 5000 lines. I want to create a variable Y in which the category of some of them would have to change to another following an equation. I have no problem until here. My problem is that if the category at Y is the same as the category at X, I want to repeat the operation and I don't know how.

I.E.

Suppose that X=5.

If "condition is respected" then Y = "equation attributing a number between 1 and 10".

There is no problem if the category for Y is anything else than 5. However, if it's 5, I need to repeat the "if" statement.

Thanks,


Accepted Solutions
Solution
‎12-08-2012 07:19 PM
Super User
Posts: 5,085

Re: Repeat codes following a condition

You have almost spelled out the solution.  SAS supports DO UNTIL, so you could do something along these lines:

if banilieue=1 then do;

   MUNIC2=0;

   do until (MUNIC2 >= MUNIC);

        MUNIC2 = MUNIC2 + 1;

        if MUNIC2=8 then MUNIC2=9;

   end;

end;

This might not be exactly what you want in terms of results, but it should be the right set of tools to apply.

Good luck.

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Repeat codes following a condition

are you looking for something like:

if y=5 then do;

  if ???

  if ???

end;

Frequent Contributor
Posts: 110

Re: Repeat codes following a condition

Of course, this code works for one time, but if Y is still 5 after the second repetition, the problem is still present. I guess I could just repeat "if y=5 then do" like 10 times, but it's not really elegant.

Super Contributor
Posts: 1,636

Re: Repeat codes following a condition

you could put your if statement in a macro, below is an example:

%macro test;

age=age+5;

%mend;

data class;

  set sashelp.class;

  if age=14 then do;

  %test

  end;

  proc print;run;

Frequent Contributor
Posts: 110

Re: Repeat codes following a condition

@Linlin Same problem as the previous. The macro is only launch one time, so if age is still 14 after running it (well... it's impossible in your example, but it is in mine), it will remain 14.

Super Contributor
Posts: 1,636

Re: Repeat codes following a condition

I understand you problem now.

PROC Star
Posts: 7,363

Re: Repeat codes following a condition

Post some sample data and desired results from that data.  Posting the code that you've tried wouldn't hurt either.

Frequent Contributor
Posts: 110

Re: Repeat codes following a condition

There is a variable MUNIC whose categories are 1-2-3-4-5-6-7-9-10 (there is no 8).

The variable MUNIC2 would be created according to those statements:

w=rand('uniform');

if banlieue=1 then do;

    if w<probbbm1 then MUNIC2=1;

    if probbbm1<=w<probbbm2 then MUNIC2=2;

    if probbbm2<=w<probbbm3 then MUNIC2=3;

    if probbbm3<=w<probbbm4 then MUNIC2=4;

    if probbbm4<=w<probbbm5 then MUNIC2=5;

    if probbbm5<=w<probbbm6 then MUNIC2=6;

    if probbbm6<=w<probbbm7 then MUNIC2=7;

    if probbbm7<=w<probbbm9 then MUNIC2=9;

    if probbbm9<=w<probbbm10 then MUNIC2=10;

end;

Thus, I want another condition saying that if MUNIC2=MUNIC then I repeat the creation of MUNIC2 until MUNIC2 /= MUNIC.

Solution
‎12-08-2012 07:19 PM
Super User
Posts: 5,085

Re: Repeat codes following a condition

You have almost spelled out the solution.  SAS supports DO UNTIL, so you could do something along these lines:

if banilieue=1 then do;

   MUNIC2=0;

   do until (MUNIC2 >= MUNIC);

        MUNIC2 = MUNIC2 + 1;

        if MUNIC2=8 then MUNIC2=9;

   end;

end;

This might not be exactly what you want in terms of results, but it should be the right set of tools to apply.

Good luck.

Frequent Contributor
Posts: 110

Re: Repeat codes following a condition

Thanks, I didn't know that SAS supports DO UNTIL. Problem solved.

Respected Advisor
Posts: 4,651

Re: Repeat codes following a condition

Seems like you want something like

do until(MUNIC2 ne MUNIC);

     w= rand...;

     if 1<w<2 then MUNIC2 = 3;

     ...

     end;

be careful not to create an infinite loop.

PG

PG
Respected Advisor
Posts: 4,651

Re: Repeat codes following a condition

Actually, it would be safer to force termination of the loop after a certain number of iterations, like this:

do i = 1 to 100 until(MUNIC2 ne MUNIC);

     w= rand...;

     if 1<w<2 then MUNIC2 = 3;

     ...

     end;

 

just in case.

PG

PG
☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 365 views
  • 6 likes
  • 5 in conversation