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,
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.
are you looking for something like:
if y=5 then do;
if ???
if ???
end;
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.
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;
@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.
I understand you problem now.
Post some sample data and desired results from that data. Posting the code that you've tried wouldn't hurt either.
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.
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.
Thanks, I didn't know that SAS supports DO UNTIL. Problem solved.
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
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
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.