BookmarkSubscribeRSS Feed
yahoo0806
Calcite | Level 5

Hi all,

I'm trying to write an iteration program and the main idea is that I want my program run multiple time and each time to use one value from a macro variable with a set of values. 

 

The example code is as below:

data critlist;
input var1 var2;
datalines;
1.12 1.19
1.12 1.29
1.12 1.36
1.12 1.41
1.22 1.19
1.22 1.29
1.22 1.36
1.22 1.41
1.32 1.19
1.32 1.29
1.32 1.36
1.32 1.41
1.42 1.19
1.42 1.29
1.42 1.36
1.42 1.41
;

proc sql noprint;
select var1, var2 into :mvar1 separated by ' ', mvar2 separated by ' '
from critlist;
quit;

 

So my question is i don't know how to write my data step so that i can put the value from the macro variable into use: 

 

data output; set input;

if ana_var > 1.12                        *Here i don't know what to so that SAS will know if the the 1st value from &mvar1;

then ana_crit = "using 1.12";      *Here i don't know what to so that SAS will know if the the 1st value from &mvar1;

 

if ana_var > 1.19                        *Here i don't know what to so that SAS will know if the the 1st value from &mvar2;

then ana_crit = "using 1.19";      *Here i don't know what to so that SAS will know if the the 1st value from &mvar2;

run;

 

then run this data step multiple times to use ALL values from &mvar1 and &mvar2.

 

so basically, i want to run my data step multiple times using each of the values i assigned to &mvar1 and &mvar2. But i don't know how to write the step....

 

Thanks!

Tina

3 REPLIES 3
ErikLund_Jensen
Rhodochrosite | Level 12

Hi Tina @yahoo0806 

I cannot figure out what you are trying to achieve, so I might be totally wrong in my solution, but the code works as a demonstration of how one can loop over the mvar lists, take the next values and use them in a data step. 

 

If I can be of any further help, please write back.

Erik

*The example code is as below:;

data critlist;
input var1 var2;
datalines;
1.12 1.19
1.12 1.29
1.12 1.36
1.12 1.41
1.22 1.19
1.22 1.29
1.22 1.36
1.22 1.41
1.32 1.19
1.32 1.29
1.32 1.36
1.32 1.41
1.42 1.19
1.42 1.29
1.42 1.36
1.42 1.41
;

data input (drop=i); /* For this example the input ds must end with a 0 (zero); */
	do i = 101 to 280 by 11;
		ana_var = i / 100;
		output;
	end;
run;

proc sql noprint;
select var1, var2 into :mvar1 separated by ' ', : mvar2 separated by ' '
from critlist;
quit;
%let loopcount = &sqlobs;

%let input = input; /* input dsname into macro variable */
%let outputprefix = out; /* prefix for numbered output ds */
%macro m;
	%do i = 1 %to &loopcount;
		%let thislow = %scan(&mvar1,&i,%str( ));
		%let thisuse = %scan(&mvar2,&i,%str( ));
		%put &=thislow &=thisuse;

		%if &i = 1 %then %let thisinput = &input;
		%else %let thisinput = &outputprefix%eval(&i-1);
		%put &=thisinput;

		data &outputprefix&i; set &thisinput;
			if ana_var > &thislow then ana_crit = catx(' ',"using","&thisuse");
		run;
	%end;
%mend;
%m;
 

 

PaigeMiller
Diamond | Level 26

Yes, please explain what you are trying to do here. I don't think you need macros at all, but then again, I don't understand the goal here.

--
Paige Miller
Tom
Super User Tom
Super User

If you have a list of value in a macro variable use %SCAN() to extract one of them.

%let i=1 ;
%put &=i %scan(&mvar1,&i,%str( ));

But why not just use your actual values instead of converting them from numbers into text and back again?

data output; 
  if _n_=1 then set crit (firstobs=&i obs=&i keep=var1 var2);
  set input;
  if ana_var > var1 then ana_crit = catx(' ','using',var1); 
  if ana_var > var2 then ana_crit = catx(' ','using',var2); 
run;

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 25. 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
  • 3 replies
  • 479 views
  • 1 like
  • 4 in conversation