<?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: Create a macro list counting values in a macro variable in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854061#M82402</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For count, I will create dummy variables which names are related to the count (like A_1, A_2,..., A_16) after each iteration.&lt;/P&gt;
&lt;P&gt;For count_l, I just store the total number of items satisfying the condition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 17 Jan 2023 09:47:41 GMT</pubDate>
    <dc:creator>windy</dc:creator>
    <dc:date>2023-01-17T09:47:41Z</dc:date>
    <item>
      <title>Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854043#M82400</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a macro variable containing a list of values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let listA =&amp;nbsp;-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 (16 values)&lt;/P&gt;
&lt;P&gt;Now I want to create macro variables called count and count_l.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Count returns the count of each item in the listA.&lt;/P&gt;
&lt;P&gt;Count =&amp;nbsp; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16&lt;/P&gt;
&lt;P&gt;Count_l returns the count of each item in the listA if values are smaller than or equal to 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Count_l = 1 2 3 4 5 6&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much in advance for any help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2023 05:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854043#M82400</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-17T05:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854053#M82401</link>
      <description>&lt;P&gt;What will you be doing with the resulting macro variables?&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2023 07:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854053#M82401</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-01-17T07:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854061#M82402</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For count, I will create dummy variables which names are related to the count (like A_1, A_2,..., A_16) after each iteration.&lt;/P&gt;
&lt;P&gt;For count_l, I just store the total number of items satisfying the condition.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2023 09:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854061#M82402</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-17T09:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854067#M82403</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297250"&gt;@windy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;For count, I will create dummy variables which names are related to the count (like A_1, A_2,..., A_16) after each iteration.&lt;/P&gt;
&lt;P&gt;For count_l, I just store the total number of items satisfying the condition.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You don't need dummy variables to obtain counts and store results. You can use PROC FREQ. You don't need macro variables either. Could you please provide even more details about what you are doing, describe the goal and the end result. It seems you are choosing a very difficult approach that simply isn't necessary.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2023 11:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854067#M82403</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-01-17T11:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854082#M82404</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let listA = -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 ;

data temp;
 do i=1 to countw("&amp;amp;listA."," ");
   value=input(scan("&amp;amp;listA.",i," "),best.);
   output;
 end;
run;
proc sort data=temp;by value;run;
data temp;
 set temp;
 count+1;
 if value&amp;lt;=0 then count_i+1;
  else count_i=.;
run;
options missing=' ';
proc sql noprint;
select count into :count separated by ' ' from temp;
select count_i into :count_i separated by ' ' from temp;
quit;

%put &amp;amp;=count. ;
%put &amp;amp;=count_i.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Jan 2023 13:10:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854082#M82404</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-01-17T13:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854085#M82405</link>
      <description>&lt;P&gt;As others have mentioned, this looks unusual as the objective of a SAS program.&amp;nbsp; My personal suspicion is that this is a Python application being migrated to SAS.&amp;nbsp; And SAS would have other, simpler ways to get to the final result (if we knew what the final result should be).&amp;nbsp; At any rate, what you ask for is not difficult:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro counts (listA=);
   %local _i_ _k_;
   %global neg_list complete_list;
   %let neg_list=;
   %let complete_list=;
   %do _i_ = 1 %to %sysfunc(countw(&amp;amp;listA));
      %let complete_list = &amp;amp;complete_list &amp;amp;_i_;
      %if %sysevalf(%scan(&amp;amp;complete_list, &amp;amp;_i_) &amp;lt;= 0) %then %do;
         %let _k_ = &amp;amp;_k_ %eval(&amp;amp;_k_ +1);
         %let neg_list = &amp;amp;neg_list &amp;amp;_k_;
      %end;
   %end;
%mend counts;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then call the macro using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%counts (list=&amp;amp;listA)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code is untested so might need a little debugging.&amp;nbsp; And it contains a few subtleties such as using %SYSEVALF instead of %EVAL in case one of the numbers in the list contains a decimal point.&amp;nbsp; (Also the +1 when no space between the plus and the 1 avoids the need to initialize _k_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the bottom line is still what others have suggested.&amp;nbsp; If we know the ultimate objective, we can guide you toward a more direct SAS-based approach.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2023 13:41:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854085#M82405</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-01-17T13:41:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854088#M82406</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Count returns the count of each item in the listA.&lt;/P&gt;
&lt;P&gt;Count =&amp;nbsp; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The meaning of the English sentence does not match the value desired.&lt;/P&gt;
&lt;P&gt;The count of the number of items in the list is just 16.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let count=%sysfunc(countw(&amp;amp;lista,%str( )));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If for some reason you want to convert that to the string you want just add a %DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you meant to say you want the make a list of the INDEXes of the items in the list instead of "count" of the items?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That would more properly match the second list you requested.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do index=1 %to %sysfunc(countw(&amp;amp;listA,%str( )));
  %let count=&amp;amp;count &amp;amp;index;
  %if %scan(&amp;amp;listA,&amp;amp;index,%str( )) &amp;lt;= 0 %then %let Count_l=&amp;amp;Count_l &amp;amp;index;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the values are also going to be ordered, like in your example, then perhaps the second could also just be reduced to a single number, the index to where the zero value appears, instead?&amp;nbsp; That can be found by using the E modifier with the FINDW() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Count_l=%sysfunc(findw(&amp;amp;lista,0,,se));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;13   %let listA = -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 ;
14   %let count=%sysfunc(countw(&amp;amp;listA,%str( )));
15   %let Count_l=%sysfunc(findw(&amp;amp;listA,0,,se));
16   %put &amp;amp;=count &amp;amp;=Count_l ;
COUNT=16 COUNT_L=6
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2023 14:10:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854088#M82406</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-01-17T14:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854100#M82407</link>
      <description>Thank you so much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. This code fits well to the macros that I am working on. Thanks for correcting me. What I need is actually the indexes of the items.</description>
      <pubDate>Tue, 17 Jan 2023 15:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854100#M82407</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-17T15:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854102#M82408</link>
      <description>You're right that I am trying to translate the codes from other language to SAS. My SAS skills related to macro is not strong, so I am pretty baffled when it comes to deal with them. Thanks so much for your help. I really appreciate.</description>
      <pubDate>Tue, 17 Jan 2023 15:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854102#M82408</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-17T15:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854106#M82409</link>
      <description>Thanks so much for your help. The codes worked very well, and I got the expected results. As I am writing a bigger macro, I chose the other option as the solution. But I learn a lot from your codes. I very appreciate it.</description>
      <pubDate>Tue, 17 Jan 2023 15:28:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854106#M82409</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-17T15:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create a macro list counting values in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854107#M82410</link>
      <description>Sorry for making a confusing request. I realized that I had overcomplicated the problems. It's because I am trying to translate from other languages to SAS. I'll be more straightforward when I make a request next time. Thank you so much for your comment. I also appreciate all the helps I got from the SAS community.</description>
      <pubDate>Tue, 17 Jan 2023 15:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-a-macro-list-counting-values-in-a-macro-variable/m-p/854107#M82410</guid>
      <dc:creator>windy</dc:creator>
      <dc:date>2023-01-17T15:36:20Z</dc:date>
    </item>
  </channel>
</rss>

