BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sami1234
Fluorite | Level 6

 

Dear All,

 

Would you please help me with the following.

 

If the sublist is empty or blank, how can it be the tackle in the following code?

 

 

%do i = 1 %to %sysfunc(countw(&list1));

%do j = 1 %to %sysfunc(countw(&sublist));

 

%let main&i. = %scan(&list1,&i,%str( ));

%let sub&j. = %scan(&sublist,&j,%str( ));

 

%if &&main&. = &&sub&j. %then %do;

/*Some steps */

%end;

%else %do;

 

/**other steps**/

 

%end;

%end;

%end;

 

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@Sami1234 

You've only posted a SAS macro code snippet so how do you expect us to know anything about "If any of the variable from list1 is not in the input dataset or misspelled" and how this would affect your processing.

 

You can always query the SAS dictionary tables (dictionary.columns) to determine if variable names listed as text (words) in a macro variable also exist in the actual data set - and then branch to some exception logic in case you find discrepancies.

You would likely implement such a check before you start the macro do loops you've posted.

 

Below just showcasing what I'm talking about.

%macro demo(varlist);
  %let varlist_quotesAndcomma="%sysfunc(prxchange(s/\s+/%nrbquote(",")/i,-1,&varlist))";
  %put &=varlist_quotesAndcomma;
  proc sql noprint;
    select count(*) into :nvars
    from dictionary.columns
    where libname='SASHELP' and memname='CLASS' and upcase(name) in (%upcase(&varlist_quotesAndcomma))
    ;
  quit;

  %if &nvars ne %sysfunc(countw(&varlist)) %then
    %do;
      %put Branch to exception logic;
    %end;
  %else
    %put Branch to standard logic;
%mend;
%demo(name sex height);
%demo(name sex heigt);

 

 

View solution in original post

6 REPLIES 6
Patrick
Opal | Level 21

@Sami1234 

I figured out the following by trying so "making up" the explanation. If the macro variable is empty then "something" must still tell the countw() function that a string has been passed in. When I've used the %str() function then things worked for me - I guess that's because %str() adds some hidden quoting characters so the string countw() gets is not really empty (because when also adding %unquote()  around the %str() function SAS throws again an Error).

%macro test(sublist);
  %do j = 1 %to %sysfunc(countw(%str(&sublist)));
    %put XXXXXXXXXXX;
  %end;
%mend;
%test(test);
%test(test blah);
%test();
Sami1234
Fluorite | Level 6

Sorry, I would like to add one more condition and how to do this?

 

1: If the sublist is empty or blank, how can it be the tackle in the following code?

2: If any of the variable from list1 is not in the input dataset or misspelled, how to end the macro and come off without running any further steps?

 

 

%do i = 1 %to %sysfunc(countw(&list1));

%do j = 1 %to %sysfunc(countw(&sublist));

 

%let main&i. = %scan(&list1,&i,%str( ));

%let sub&j. = %scan(&sublist,&j,%str( ));

 

%if &&main&. = &&sub&j. %then %do;

/*Some steps */

%end;

%else %do;

 

/**other steps**/

 

%end;

%end;

%end;

 

 

Thank you!

Patrick
Opal | Level 21

@Sami1234 

You've only posted a SAS macro code snippet so how do you expect us to know anything about "If any of the variable from list1 is not in the input dataset or misspelled" and how this would affect your processing.

 

You can always query the SAS dictionary tables (dictionary.columns) to determine if variable names listed as text (words) in a macro variable also exist in the actual data set - and then branch to some exception logic in case you find discrepancies.

You would likely implement such a check before you start the macro do loops you've posted.

 

Below just showcasing what I'm talking about.

%macro demo(varlist);
  %let varlist_quotesAndcomma="%sysfunc(prxchange(s/\s+/%nrbquote(",")/i,-1,&varlist))";
  %put &=varlist_quotesAndcomma;
  proc sql noprint;
    select count(*) into :nvars
    from dictionary.columns
    where libname='SASHELP' and memname='CLASS' and upcase(name) in (%upcase(&varlist_quotesAndcomma))
    ;
  quit;

  %if &nvars ne %sysfunc(countw(&varlist)) %then
    %do;
      %put Branch to exception logic;
    %end;
  %else
    %put Branch to standard logic;
%mend;
%demo(name sex height);
%demo(name sex heigt);

 

 

Sami1234
Fluorite | Level 6

Thank you so much, its working perfectly fine now 🙂

Tom
Super User Tom
Super User

To prevent errors from %SYSFUNC() you can just add a comma.

645   %let x=;
646   %put %sysfunc(countw(&x));
ERROR: The function COUNTW referenced by the %SYSFUNC or %QSYSFUNC macro function has too few arguments.
.
647   %put %sysfunc(countw(&x,));
0
Sami1234
Fluorite | Level 6

Thanks for the reply.

if I put 'Comma', it's not throwing any error at all and skipping the whole macro.

But I would like to achieve something as below.

 

list1 contains a list of continuous and categorical variables altogether & sublist contains only continuous variables. So if the variable in the sublist is empty, it's not running either step, so If I put %let sublist=%str(blank); it's working fine.

But if the sublist contains more then 2 variables then 'else do' step runs twice.

Would it be possible to put a stop or condition on the loop that it should only run once for list1 variables?

 

 

%do i = 1 %to %sysfunc(countw(&list1));

%do j = 1 %to %sysfunc(countw(&sublist));

 

%let main&i. = %scan(&list1,&i,%str( ));

%let sub&j. = %scan(&sublist,&j,%str( ));

 

%if &&main&. = &&sub&j. %then %do;

/*Some steps */

%end;

%else %do;

 

/**other steps**/

 

%end;

%end;

%end;

 

 

Thank you!

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 1843 views
  • 0 likes
  • 3 in conversation