I am sorry I could not test the code (that I suggested) as I was away from my desktop. There have been very nice alternative solution in this post, using arrays. They are elegant codes and will work here. However, I am doing some cheap selling of my method here. Apologies for that but thought you should know. May be useful in some cases. Suppose the problem becomes complex where you have to exclude "one" number (say 3) from a large set of possible numbers (say 1 to 1000), then creating an array of 1000 numbers could be a little time consuming and memory consuming. However if you you use a DO WHILE loop, it will work well. Supposing I want to create a Y column with random number ranging from 1 to 1000 but number 3 excluded, then I will write the below code. DATA Number_3_Exluded_From_1_to_1000;
SET sashelp.cars;
Y=RAND('integer', 1, 1000);
DO WHILE(Y=3);
Y=RAND('integer', 1, 1000);
END;
RUN; When you run this code it has following CPU and memory footprints. NOTE: There were 428 observations read from the data set SASHELP.CARS.
NOTE: The data set WORK.NUMBER_3_EXLUDED_FROM_1_TO_1000 has 428 observations and 16 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.01 seconds
system cpu time 0.00 seconds
memory 840.96k
OS Memory 28584.00k
Timestamp 06/25/2019 03:32:00 PM
Step Count 30 Switch Count 2
Page Faults 0
Page Reclaims 168
Page Swaps 0
Voluntary Context Switches 10
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264 The number 3 has been excluded from the column Y. You can check for that using a simple PROC SQL as below. PROC SQL;
SELECT Y
FROM Number_3_Exluded_From_1_to_1000
WHERE Y=3;
QUIT; The log clearly says that no rows were selected as below. 1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
70
71 PROC SQL;
72 SELECT Y
73 FROM Number_3_Exluded_From_1_to_1000
74 WHERE Y=3;
NOTE: No rows were selected.
75 QUIT; I might be wrong here and I am sure there is a more elegant solution to the problem I have stated above. I would love to learn something new on this. So what I am suggesting to you is run the below code. Replace everything after the IF THEN statement (in your original post) Y=rand('integer', 0, 7);
Do while (Y=3);
Y=Rand('integer', 0, 7);
End;
Output;
RUN; Best wishes.
... View more