Hello,
I am trying to write a program which will loop over an observation for a varying number of times, depending on the contents of this observation. So something like:
data variable_length_loop;
set sashelp.cars;
if Origin EQ "Asia" then
do i = 1, 2;
else if Origin EQ "Europe" then
do i = 3;
Origin_short = substr(Origin, 1, i);
output;
end;
run;
I can obviously get it to work by repeating the code block between do and end:
data variable_length_loop;
set sashelp.cars;
if Origin EQ "Asia" then
do i = 1, 2;
Origin_short = substr(Origin, 1, i);
output;
end;
else if Origin EQ "Europe" then
do i = 3;
Origin_short = substr(Origin, 1, i);
output;
end;
run;
The problem is that this leads to a lot of copypasting with increased complexity of the code and the number of the conditions to cover. Is this possible without resorting to macros? Thank you for your feedback in advance.
This seems to be part of a bigger problem, am I right? What are you trying to accomplish?
This seems to be part of a bigger problem, am I right? What are you trying to accomplish?
I am trying to calculate frequencies of events. The denominator varies, and some frequencies need to be calculated for more than one denominator. I guess I could just calculate all combinations and filter the ones I don't need out, but this seems rather wasteful.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.