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.

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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