<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help understanding what is happening in this macro? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707684#M26537</link>
    <description>&lt;P&gt;I don't think this code will do anything.&amp;nbsp; The code as posted has both syntax errors and logic errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears to be an attempt to generate a list of variables to drop by counting how many times the variable has a value of 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro is creating macro variables without ever making sure they are defined external to the macro.&amp;nbsp; So macro variables like DROP_LIST will be created as local if they have not already been created before the macro is called.&amp;nbsp; You might add this to the top of the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local flag_list i flag triggered  ;
%if not %symexist(drop_list) %then %global drop_list ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This line has misplaced semi-colons:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF &amp;amp;triggered=0 AND drop_list NE %STR() %THEN; %LET drop_list=%SYSFUNC(CATX(%str( ),&amp;amp;drop_list,&amp;amp;flag)) %ELSE %IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;flag;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Perhaps they meant:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF &amp;amp;triggered=0 AND drop_list NE %STR() %THEN %LET drop_list=%SYSFUNC(CATX(%str( ),&amp;amp;drop_list,&amp;amp;flag));
%ELSE %IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;flag;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But really all they needed was:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;drop_list &amp;amp;flag;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is no need to try to use any of the CAT series functions in macro code.&amp;nbsp; Just expand the macro variables where you want their values to appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no need to use UPCASE() function on the LIBNAME or MEMNAME fields in SASHELP.VCOLUMN as those values are always upper case.&lt;/P&gt;</description>
    <pubDate>Tue, 22 Dec 2020 15:21:12 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-12-22T15:21:12Z</dc:date>
    <item>
      <title>Help understanding what is happening in this macro?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707666#M26534</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;
%MACRO dropEmptyFlags(dataset_name);
        DATA WORK.flag_names (KEEP=name);
                SET SASHELP.VCOLUMN;
                WHERE ( libname='WORK' AND UPCASE(memname)=UPCASE("&amp;amp;dataset_name") ) AND ( UPCASE(name) LIKE 'CE_%' OR UPCASE(name) LIKE 'SM_%');
        RUN;
        PROC SQL;
                SELECT DISTINCT NAME INTO :flags SEPARATED BY ' '
                FROM WORK.flag_names;
        QUIT;
        %LET flag_list=&amp;amp;flags;
        %LET drop_list=%STR();
        %DO i=1 %TO %SYSFUNC(COUNTW(&amp;amp;flags));
                %LET flag=%SCAN(&amp;amp;flags,&amp;amp;i);
                PROC SQL;
                        SELECT COUNT(&amp;amp;flag) INTO :triggered
                        FROM WORK.&amp;amp;dataset_name
                        WHERE &amp;amp;flag=1;
                QUIT;
                %IF &amp;amp;triggered=0 AND drop_list NE %STR() %THEN; %LET drop_list=%SYSFUNC(CATX(%str( ),&amp;amp;drop_list,&amp;amp;flag)) %ELSE %IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;flag;
        %END;
        %PUT drop list is %SYSFUNC(COUNTW(&amp;amp;drop_list)) long out of %SYSFUNC(COUNTW(&amp;amp;flags)): SET WORK.&amp;amp;dataset_name(DROP=&amp;amp;drop_list);
%MEND;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I inherited this code from a colleague and am trying to understand what it does. I do not understand where the data is coming from for this macro, first and foremost, as work.Flag_names does not exist in my work library after running this code and doesn't exist anywhere else in the program. Could anybody help me decode this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 14:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707666#M26534</guid>
      <dc:creator>ivymckeeSTC</dc:creator>
      <dc:date>2020-12-22T14:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding what is happening in this macro?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707678#M26535</link>
      <description>&lt;P&gt;I can give you a step or two, but you are really in over your head.&amp;nbsp; You would need to understand more SAS (not macro language) to figure out what is happening here.&amp;nbsp; For example, you are wondering where work.flag_names comes from when the answer is that the program creates it.&amp;nbsp; So more SAS knowledge is mandatory.&amp;nbsp; More macro language experience would also be needed.&amp;nbsp; For example, did you know that running this code does not execute anything?&amp;nbsp; It merely defines what the command %dropEmptyFlags should do.&amp;nbsp; To execute anything, you need to later execute %dropEmptyFlags.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you execute %dropEmpty_Flags, you supply a value for DATASET_NAME.&amp;nbsp; This should be the name of an existing data set in the WORK library.&amp;nbsp; From that data set, the program creates FLAG_NAMES in the WORK library, containing the names of all variables that begin with "CE_" or "SM_".&amp;nbsp; (Capitalization is not important.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That takes you through the first half of the program.&amp;nbsp; Good luck in your quest.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 15:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707678#M26535</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-12-22T15:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding what is happening in this macro?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707683#M26536</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361650"&gt;@ivymckeeSTC&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually, the macro doesn't do much - It looks like a small utility that output text in the SAS Log like "&lt;FONT face="courier new,courier"&gt;drop list is 1 long out of 2: SET WORK.class(DROP=SM_weight)&lt;/FONT&gt;"&lt;/P&gt;
&lt;P&gt;Perhaps the intention is to copy this text "&lt;FONT face="courier new,courier"&gt;SET WORK.class(DROP=SM_weight)&lt;/FONT&gt;"&amp;nbsp; and run it manually afterwards to clean up the dataset ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Macro does not need a dataset named flag_name, as it's generated by the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It just checks if your work dataset contains a column that named with CE_ or SM (in the beginning of the column name) and if they contain a value of 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps if you run this, you will understand the macro a little better. - look for&amp;nbsp; "&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;drop list&lt;/STRONG&gt;"&lt;FONT face="arial,helvetica,sans-serif"&gt; in the SAS Log.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO dropEmptyFlags(dataset_name);
        DATA WORK.flag_names (KEEP=name);
                SET SASHELP.VCOLUMN;
                WHERE ( libname='WORK' AND UPCASE(memname)=UPCASE("&amp;amp;dataset_name") ) AND ( UPCASE(name) LIKE 'CE_%' OR UPCASE(name) LIKE 'SM_%');
        RUN;
        PROC SQL;
                SELECT DISTINCT NAME INTO :flags SEPARATED BY ' '
                FROM WORK.flag_names;
        QUIT;
        %LET flag_list=&amp;amp;flags;
        %LET drop_list=%STR();

        %DO i=1 %TO %SYSFUNC(COUNTW(&amp;amp;flags));
                %LET flag=%SCAN(&amp;amp;flags,&amp;amp;i);
                PROC SQL;
                        SELECT COUNT(&amp;amp;flag) INTO :triggered
                        FROM WORK.&amp;amp;dataset_name
                        WHERE &amp;amp;flag=1;
                QUIT;
                %put triggered=&amp;amp;triggered;
                %IF &amp;amp;triggered=0 AND drop_list NE %STR() %THEN %LET drop_list=%SYSFUNC(CATX(%str( ),&amp;amp;drop_list,&amp;amp;flag)); %ELSE %IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;flag;
        %END;

        %PUT drop list is %SYSFUNC(COUNTW(&amp;amp;drop_list)) long out of %SYSFUNC(COUNTW(&amp;amp;flags)): SET WORK.&amp;amp;dataset_name(DROP=&amp;amp;drop_list);
%MEND;

data work.class;
  set sashelp.class;
  IF height =  59 then CE_height=1;
  SM_weight=0;
run;

%dropEmptyFlags(class);


data work.class;
  set sashelp.class;
  if weight = 90 then SM_weight=1;
run;

%dropEmptyFlags(class);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will find these two lines somewhere in your SAS LOG.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;drop list is 1 long out of 2: SET WORK.class(DROP=SM_weight)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;drop list is 0 long out of 1: SET WORK.class(DROP=)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 15:32:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707683#M26536</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2020-12-22T15:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding what is happening in this macro?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707684#M26537</link>
      <description>&lt;P&gt;I don't think this code will do anything.&amp;nbsp; The code as posted has both syntax errors and logic errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears to be an attempt to generate a list of variables to drop by counting how many times the variable has a value of 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro is creating macro variables without ever making sure they are defined external to the macro.&amp;nbsp; So macro variables like DROP_LIST will be created as local if they have not already been created before the macro is called.&amp;nbsp; You might add this to the top of the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%local flag_list i flag triggered  ;
%if not %symexist(drop_list) %then %global drop_list ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This line has misplaced semi-colons:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF &amp;amp;triggered=0 AND drop_list NE %STR() %THEN; %LET drop_list=%SYSFUNC(CATX(%str( ),&amp;amp;drop_list,&amp;amp;flag)) %ELSE %IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;flag;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Perhaps they meant:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF &amp;amp;triggered=0 AND drop_list NE %STR() %THEN %LET drop_list=%SYSFUNC(CATX(%str( ),&amp;amp;drop_list,&amp;amp;flag));
%ELSE %IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;flag;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But really all they needed was:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%IF &amp;amp;triggered=0 %THEN %LET drop_list=&amp;amp;drop_list &amp;amp;flag;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is no need to try to use any of the CAT series functions in macro code.&amp;nbsp; Just expand the macro variables where you want their values to appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no need to use UPCASE() function on the LIBNAME or MEMNAME fields in SASHELP.VCOLUMN as those values are always upper case.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 15:21:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Help-understanding-what-is-happening-in-this-macro/m-p/707684#M26537</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-22T15:21:12Z</dc:date>
    </item>
  </channel>
</rss>

