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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

11 REPLIES 11
Linlin
Lapis Lazuli | Level 10

are you looking for something like:

if y=5 then do;

  if ???

  if ???

end;

Demographer
Pyrite | Level 9

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.

Linlin
Lapis Lazuli | Level 10

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;

Demographer
Pyrite | Level 9

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

Linlin
Lapis Lazuli | Level 10

I understand you problem now.

art297
Opal | Level 21

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

Demographer
Pyrite | Level 9

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.

Astounding
PROC Star

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.

Demographer
Pyrite | Level 9

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

PGStats
Opal | Level 21

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
PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 11 replies
  • 1182 views
  • 6 likes
  • 5 in conversation