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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.