<?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 multiple macro variables to store counts in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853540#M337376</link>
    <description>&lt;P&gt;Why do you have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That means you re not actually reading in the data from work.have. If you remove that, it works fine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id $ armn agen @@;
 cards;
A 0 1
B 0 2
C 0 3
D 1 4
E 1 1
F 0 3
G 1 4
H 1 5
;
run;

data _null_;
 /*if 0 then */ set have nobs= N;
 call symputx(cats('N', AGEN, ARMN), N, 'L');
run;

%put _user_ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Note that "works" means runs without errors.&amp;nbsp; But it will give the same value to every macro var. Which is probably not what you want.&amp;nbsp; If you want to count distinct ID's within groups, you could use by-group processing, something like (untested):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have ;
  by armn agen id;
  if first.agen then count=0;
  if first.id then count++1;
  if last.agen then call symputx(cats('N', AGEN, ARMN), count, 'L');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jan 2023 19:01:19 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-01-12T19:01:19Z</dc:date>
    <item>
      <title>Create multiple macro variables to store counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853525#M337368</link>
      <description>&lt;P&gt;I want to be able to reference different macro variables to add totals to a header like so: Group 1 (N=&amp;amp;N11.), Group 2 (N=&amp;amp;N12.) as so on...&lt;/P&gt;
&lt;P&gt;Is there a way to do this all in a single SQL or DATA STEP?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id $ armn agen @@;
 cards;
A 0 1
B 0 2
C 0 3
D 1 4
E 1 1
F 0 3
G 1 4
H 1 5
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Attempt with DATA STEP&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
 if 0 then set have nobs= N;
 call symputx(cats('N', AGEN, ARMN), N, 'L');
run;
ERROR: Symbolic variable name N-- must contain only letters, digits, and underscores.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I know you can do something like below with PROC SQL, but I'd rather have the variable name associated with ARMN and AGEN.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
 select count(distinct ID) into :n1-:n8 from have group by ARMN, AGEN;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Jan 2023 18:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853525#M337368</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2023-01-12T18:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple macro variables to store counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853534#M337373</link>
      <description>&lt;P&gt;In your DATA step, what is the name(s) of the macro variable(s) you want to create?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This message is telling you that the values of ARMN and AGEN contain characters that are not legal for naming a macro variable.&amp;nbsp; It might be as simple as embedded blanks within ARMN and GEN, which the compress function can handle.&amp;nbsp; CATS won't remove embedded blanks.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2023 18:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853534#M337373</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-01-12T18:31:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple macro variables to store counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853540#M337376</link>
      <description>&lt;P&gt;Why do you have:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That means you re not actually reading in the data from work.have. If you remove that, it works fine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input id $ armn agen @@;
 cards;
A 0 1
B 0 2
C 0 3
D 1 4
E 1 1
F 0 3
G 1 4
H 1 5
;
run;

data _null_;
 /*if 0 then */ set have nobs= N;
 call symputx(cats('N', AGEN, ARMN), N, 'L');
run;

%put _user_ ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Note that "works" means runs without errors.&amp;nbsp; But it will give the same value to every macro var. Which is probably not what you want.&amp;nbsp; If you want to count distinct ID's within groups, you could use by-group processing, something like (untested):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have ;
  by armn agen id;
  if first.agen then count=0;
  if first.id then count++1;
  if last.agen then call symputx(cats('N', AGEN, ARMN), count, 'L');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2023 19:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853540#M337376</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-01-12T19:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple macro variables to store counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853588#M337395</link>
      <description>I would want the variable to be &amp;amp;N[ARMN][AGEN]. For example, the count of&lt;BR /&gt;subjects with ARMN=1 and AGEN=3 to be stored as &amp;amp;N13.&lt;BR /&gt;</description>
      <pubDate>Thu, 12 Jan 2023 22:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853588#M337395</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2023-01-12T22:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple macro variables to store counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853615#M337405</link>
      <description>&lt;P&gt;Storing data in macro variable often leads to unnecessary complexity in the following steps. You may want to explain the next steps, so that we could suggest a better approach.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 06:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/853615#M337405</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-01-13T06:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create multiple macro variables to store counts</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/854926#M337916</link>
      <description>&lt;P&gt;I want to use them in PROC REPORT, so I can have counts in the headers.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
 input VAR ARM1AGE1 ARM1AGE2 ARM2AGE1 ARM2AGE2 @@;
 cards;
Var1 7 4
Var2 3 9
Var3 8 12
;
run;

proc report data= WANT splitchar= '*';
 columns VAR ("Group 1" ARM1AGE1 ARM1AGE2) ("Group 2" ARM2AGE1 ARM2AGE2);
 define VAR / display "Variable";
 define ARM1AGE1 / display "&amp;lt;18yo*(N=&amp;amp;N11.)";
 define ARM1AGE2/ display "&amp;gt;=18yo*(N=&amp;amp;N12.)";
 define ARM2AGE1 / display "&amp;lt;18yo*(N=&amp;amp;N21.)";
 define ARM2AGE2 / display "&amp;gt;=18yo*(N=&amp;amp;N22.)";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Jan 2023 22:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-multiple-macro-variables-to-store-counts/m-p/854926#M337916</guid>
      <dc:creator>mariko5797</dc:creator>
      <dc:date>2023-01-20T22:00:29Z</dc:date>
    </item>
  </channel>
</rss>

