DATA Step, Macro, Functions and more

Macro Do Loops not working

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Macro Do Loops not working

Hello All,

 

I try to distinguish some variables whether they are succesful or not. To do this I wrote the below macro but it does not work.

flag_&var_used. is not accepted as a variable name in SAS. I also do not get any result neither. I am stuck in this macro.

 

Help appreciated,

 

Thanks

 

%macro success();
%let nitems = %sysfunc(countw(&var_list));
%do i=1 %to &nitems;
%let var_used = %scan((&var_list),&i,'|');
	%if (&var_used ne  'VAR1' or &var_used ne 'VAR2' or &var_used ne 'VAR3') %then %do;
		data new_&i.;
		set old;	
			if &var_used gt 0.01 then flag_&var_used. = 'succesful';
			else flag_&var_used. = 'unsuccessful'; 
		run;
	%end;
%end;
%mend;

Accepted Solutions
Solution
‎01-03-2017 05:42 AM
Super User
Posts: 5,503

Re: Macro Do Loops not working

Posted in reply to fuatengin

In addition to removing the extra quotes, you need to remove the extra parentheses.  This is the first parameter to %SCAN:

 

(&var_list)

 

The parentheses are part of the text that %SCAN examines.  The first word in the list contains a left-hand parenthesis, and the last word contains a right-hand parenthesis.

 

A smaller issue ... the length of the flag_ variable should be set ahead of time.  Otherwise you will lose the "ul" from the end of "unsuccessful".

 

Also, you might want to consider whether you could do this in one DATA step, rather than using a separate DATA step for each variable. If you want to go that route, it would be easier to create &VAR_LIST with blanks instead of pipes as the separators.  That would support this type of statement:

 

array vars {*} &var_list; 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,558

Re: Macro Do Loops not working

Posted in reply to fuatengin

 your code:

&var_used ne  'VAR1' or &var_used ne 'VAR2' or &var_used ne 'VAR3'

   - macro variables are characters, so quotes are extra

   -  if &var_used = VAR1 then it is ne VAR2. Your condition is always TRUE;

 

   change and try:

&var_used ne  VAR1 and &var_used ne VAR2 and &var_used ne VAR3

Please post your log in case of error meesages.

 

Occasional Contributor
Posts: 11

Re: Macro Do Loops not working

Hello, Thank you for your post. I changed or's with and's. in my code.

Solution
‎01-03-2017 05:42 AM
Super User
Posts: 5,503

Re: Macro Do Loops not working

Posted in reply to fuatengin

In addition to removing the extra quotes, you need to remove the extra parentheses.  This is the first parameter to %SCAN:

 

(&var_list)

 

The parentheses are part of the text that %SCAN examines.  The first word in the list contains a left-hand parenthesis, and the last word contains a right-hand parenthesis.

 

A smaller issue ... the length of the flag_ variable should be set ahead of time.  Otherwise you will lose the "ul" from the end of "unsuccessful".

 

Also, you might want to consider whether you could do this in one DATA step, rather than using a separate DATA step for each variable. If you want to go that route, it would be easier to create &VAR_LIST with blanks instead of pipes as the separators.  That would support this type of statement:

 

array vars {*} &var_list; 

Super User
Posts: 11,343

Re: Macro Do Loops not working

Posted in reply to fuatengin

Is there a specific reason you are going through the same data set and creating multiple data sets instead of one pass that could create all of the variables?

 

I also suggest that instead of creating a text variable that you might consider

 = (&var_used gt 0.01);

This will create a numeric coded variable with 1 for true (or successful or yes or what have you) and 0 for false/unsuccesful/no.

Then you can Sum the flag_&var_used to get a count of successful and a mean would be the percentage succesful in decimal form. Which may make some forms of reporting easier to write.

Occasional Contributor
Posts: 11

Re: Macro Do Loops not working

Hello ballardw, No I do not have any specific reason to create multiple datasets. That's why I changed my code. Thank you

Occasional Contributor
Posts: 11

Re: Macro Do Loops not working

Posted in reply to fuatengin

Hello All,

 

Sorry for late reply. I changed my code structure according to your advices and now it is working. I appreciate your help.

 

Thanks!

%macro success();
%let nitems = %sysfunc(countw(&var_list));
%let counter = %sysevalf(&nitems);
data new;
set old;
	%do i=1 %to &counter;
		%let var_used = %scan(&var_list,&i,'|');
		%if '&var_used.' ne  'VAR1' AND '&var_used.' ne 'VAR2' AND '&var_used.' ne 'VAR3' %then %do;	
			if &var_used gt 0.01 then flag_&var_used. = 'successful';
			else flag_&var_used. = 'unsuccesful'; 
		%end;
	%end;
run;
%mend;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 318 views
  • 2 likes
  • 4 in conversation