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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

This seems to be part of a bigger problem, am I right? What are you trying to accomplish?

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

This seems to be part of a bigger problem, am I right? What are you trying to accomplish?

js5
Pyrite | Level 9 js5
Pyrite | Level 9

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 773 views
  • 0 likes
  • 2 in conversation