<?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: How do I create a macro do loop that sets a macro variable based on an entry in a dataset? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902016#M356431</link>
    <description>It's likely that CALL EXECUTE would simplify the process (instead of CALL SYMPUT).  But we would need to know more about the analysis that gets performed for each REGION.</description>
    <pubDate>Wed, 08 Nov 2023 10:03:04 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2023-11-08T10:03:04Z</dc:date>
    <item>
      <title>How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902006#M356423</link>
      <description>&lt;P&gt;Hello! I'm rather new at SAS and I'm a little stumped on how best to do this.&lt;/P&gt;&lt;P&gt;I have the following dataset, called region_input (apologies for using a photo, the data step version SAS macro doesn't work on my SAS due to needing to get github information)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Untitled.png" style="width: 247px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89520iEBDDD7EAF829F8FB/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled.png" alt="Untitled.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I want to create a DO loop that takes the value of the Region column at each row, assigns that to a macro variable, and then performs a number of other steps using that macro variable, before moving on to the next row and repeating.&lt;BR /&gt;I am having a lot of trouble figuring out how to reference the first row, however. So far I have :&lt;/P&gt;&lt;PRE&gt;%macro importer;
%do i=1 to 7;
data _null_;
set region_input;
call symput("region_name", Region);
run;
%end;
%mend importer;&lt;/PRE&gt;&lt;P&gt;But this doesn't work and I am utterly stumped on how to progress, or even really what to search in the help files.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 05:17:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902006#M356423</guid>
      <dc:creator>Carmen_Miranda</dc:creator>
      <dc:date>2023-11-08T05:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902013#M356428</link>
      <description>&lt;P&gt;You want sthg. like this??&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
 if 0 then set work.region_input nobs=count;
 call symput('NumberOfRegions',trim(left(put(count,12.))));
 STOP;
run;
%PUT &amp;amp;=NumberOfRegions;

%macro importer;
 %do i=1 %to &amp;amp;NumberOfRegions.;
data _null_;
 set work.region_input(firstobs=&amp;amp;i. obs=&amp;amp;i.);
 call symputx("region_name", Region);
run;
%PUT &amp;amp;=region_name;
 %end;
%mend  importer;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 09:36:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902013#M356428</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-11-08T09:36:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902016#M356431</link>
      <description>It's likely that CALL EXECUTE would simplify the process (instead of CALL SYMPUT).  But we would need to know more about the analysis that gets performed for each REGION.</description>
      <pubDate>Wed, 08 Nov 2023 10:03:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902016#M356431</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-11-08T10:03:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902019#M356434</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;It's likely that CALL EXECUTE would simplify the process (instead of CALL SYMPUT). But we would need to know more about the analysis that gets performed for each REGION.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I absolutely agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;.&lt;BR /&gt;Although I don't like&amp;nbsp;CALL EXECUTE that much.&lt;BR /&gt;I always do "&lt;STRONG&gt;data-driven dynamic code generation&lt;/STRONG&gt;" instead ...&lt;/P&gt;
&lt;P&gt;using data _NULL_ step with File stmt. and several Put statements&lt;/P&gt;
&lt;P&gt;followed by a %INCLUDE of the generated code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 10:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902019#M356434</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2023-11-08T10:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902026#M356437</link>
      <description>&lt;P&gt;How about this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro importer;
proc sql noprint;
    select region into :regions separated by ' ' from region_input;
quit;
%do i=1 to &amp;amp;sqlobs;
%let this_region=%scan(&amp;amp;regions,&amp;amp;i,%str( ));
/* Your analysis of each region goes here */
%end;
%mend importer;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which brings us to the big question in my mind — why not use BY statements, and perform analyses with the command&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;BY REGION;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All of this other manipulation in my code at the top, and everyone else's code, needs to be justified by answering: Why doesn't BY work here?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 12:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902026#M356437</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-11-08T12:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902207#M356516</link>
      <description>&lt;P&gt;Oh wow, this one's really clever! I'm not very familiar with SAS SQL, but this works really well.&lt;BR /&gt;&lt;BR /&gt;There is one specific reason why I'm not using BY: I actually kinda forgot it existed. I started working in SAS 3 weeks ago and I've had so many new things put into my brain that I'm forgetting the first things I learned! Something of a grim start to the language, eh?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for the help with this. I'll give BY a shot and see if I can get it working!&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 22:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902207#M356516</guid>
      <dc:creator>Carmen_Miranda</dc:creator>
      <dc:date>2023-11-08T22:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902208#M356517</link>
      <description>&lt;P&gt;Oh, I see how this works! That's super clever, I think the tutorial I had been following skipped over the firstobs option in set statements. (That or I've forgotten it already...)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for the help. I think the way of dynamically adjusting the number of regions is really awesome, too, as I was going to have to figure that one out myself down the line.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2023 22:10:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902208#M356517</guid>
      <dc:creator>Carmen_Miranda</dc:creator>
      <dc:date>2023-11-08T22:10:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902210#M356519</link>
      <description>&lt;P&gt;DO NOT use the &lt;STRONG&gt;ANCIENT&lt;/STRONG&gt; CALL SYMPUT() function unless it is critical that leading and/or trailing spaces&amp;nbsp; actually be put into the macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the &lt;STRONG&gt;NORMAL&lt;/STRONG&gt; CALL SYMPUTX() function instead.&amp;nbsp; It has only been around about 25 years now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also look at the CATS() function for generating a unique macro variable name for each observation's value of REGION.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set region_input;
  call symputx(cats('region_name',_n_),Region);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Nov 2023 22:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902210#M356519</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-11-08T22:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do I create a macro do loop that sets a macro variable based on an entry in a dataset?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902258#M356541</link>
      <description>&lt;P&gt;Oh, that's super cool! I haven't heard of CATS() before, I think that's going to be super useful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also I have the sneaking suspicion that my organisation's in-house SAS tutorial might actually predate me as a person, so I haven't heard of SYMPUTX() yet. Thanks for the head's up on that one, I certainly don't need any leading or trailing blanks that I hadn't noticed messing up my code later on.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 04:41:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-create-a-macro-do-loop-that-sets-a-macro-variable-based/m-p/902258#M356541</guid>
      <dc:creator>Carmen_Miranda</dc:creator>
      <dc:date>2023-11-09T04:41:05Z</dc:date>
    </item>
  </channel>
</rss>

