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;
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;
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.
Hello, Thank you for your post. I changed or's with and's. in my code.
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;
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.
Hello ballardw, No I do not have any specific reason to create multiple datasets. That's why I changed my code. Thank you
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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.