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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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