<?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: Referencing SAS Macro Variable in Data Subsetting in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885621#M39293</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I want to subset each of this data into a temporary table for each pass through to run a paired T-test on each class, however I am having issues with sub setting the data.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Subsetting the data into separate data sets is usually unnecessary in SAS.&amp;nbsp;Performing paired t-tests on each data set is the wrong approach. Instead, use the BY statement in PROC TTEST. If you do that, no sub-setting is needed, only one data set is needed, and no macro variables are needed, no DO loops are needed.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Jul 2023 14:14:11 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-07-20T14:14:11Z</dc:date>
    <item>
      <title>Referencing SAS Macro Variable in Data Subsetting</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885618#M39292</link>
      <description>&lt;P&gt;Hello new to SAS, and I am running a macro that loops through the different classes in a variable, in this case, urban, rural, and national.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to subset each of this data into a temporary table for each pass through to run a paired T-test on each class, however I am having issues with sub setting the data.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(This is just a snapshot of the data, there are also other variables that I want to group by excluded)&amp;nbsp;&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV align="center"&gt;Obs Geography Var1 Var2123 &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Urban&lt;/TD&gt;&lt;TD&gt;-0.01438&lt;/TD&gt;&lt;TD&gt;-0.12000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Rural&lt;/TD&gt;&lt;TD&gt;-0.08528&lt;/TD&gt;&lt;TD&gt;-0.02000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;National&lt;/TD&gt;&lt;TD&gt;-0.03121&lt;/TD&gt;&lt;TD&gt;-0.10000&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P class="lia-align-left"&gt;The code in the macro that does this&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;The new dataset that is created is ClassType, which just includes either just all of the "Urban" "Rural" or "National" data&lt;/P&gt;&lt;P class="lia-align-left"&gt;group_var = Geography (In this case) and ClassVar stores the observations of Geography&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;%MACRO ttest_by_class(ds, group_var, class_vars, numeric_var1, numeric_var2, output);

%LET num_class_vars = %SYSFUNC(COUNTW(&amp;amp;class_vars.));
%DO i = 1 %TO &amp;amp;num_class_vars.;

     %LET ClassVar = %SCAN(&amp;amp;class_vars, &amp;amp;i, ' ');
     DATA ClassType;
         SET &amp;amp;ds.;
         IF(&amp;amp;group_var. = &amp;amp;ClassVar.);
     RUN;

...&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;However, when I run the Data step, the tables that it produces contains are empty, but when I change &amp;amp;ClassVar. to the individual strings "Urban" , it works fine. Is this just not possible with macro variables, or is there a way to convert to string?&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;Also if there an easier way to do what I am planning on doing, I would like to know.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2023 13:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885618#M39292</guid>
      <dc:creator>LC11</dc:creator>
      <dc:date>2023-07-20T13:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing SAS Macro Variable in Data Subsetting</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885621#M39293</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I want to subset each of this data into a temporary table for each pass through to run a paired T-test on each class, however I am having issues with sub setting the data.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Subsetting the data into separate data sets is usually unnecessary in SAS.&amp;nbsp;Performing paired t-tests on each data set is the wrong approach. Instead, use the BY statement in PROC TTEST. If you do that, no sub-setting is needed, only one data set is needed, and no macro variables are needed, no DO loops are needed.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2023 14:14:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885621#M39293</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-20T14:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing SAS Macro Variable in Data Subsetting</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885624#M39294</link>
      <description>&lt;P&gt;You didn't show your macro call, so it's hard to see the mistake you made.&amp;nbsp; Definitely it's possible to do that sort of looping and subsetting with a macro.&amp;nbsp; But since you're new to SAS, and you (sensibly) asked for easier alternative ways, in SAS it is rarely necessary (or even helpful) to split data into subset-specific tables like this.&amp;nbsp; Instead of looping over data sets, you almost always are better off having one dataset with a grouping variable, and then use that variable in a BY statement for by-group processing.&amp;nbsp; e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort dat=sashelp.class out=class ;
  by sex ;
run ;

proc ttest data=class ;
  var height ;
  by sex ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So that will run a one-sample ttest on sex=F subset, then run it on sex=M.&amp;nbsp; In order to use BY group processing, your data need to be sorted by the BY variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The canonical paper on BY group processing is:&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/forum2007/183-2007.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/forum2007/183-2007.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2023 14:19:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885624#M39294</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-07-20T14:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing SAS Macro Variable in Data Subsetting</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885634#M39295</link>
      <description>&lt;P&gt;What did you put into the macro variable Class_vars?&lt;/P&gt;
&lt;P&gt;That will determine what gets put into the macro variable&amp;nbsp;ClassVar .&lt;/P&gt;
&lt;P&gt;Which will determine if using it this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; IF(&amp;amp;group_var. = &amp;amp;ClassVar.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Makes any sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if CLASS_VARS = Urban Rural National and GROUP_VAR=&amp;nbsp;Geography then you will end up generating code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF (Geography = Urban) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which is valid SAS syntax that will compare the value of the variable GEOGRAPHY to the value of the variable URBAN.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you printout of the data did not have variable named URBAN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need to generate code like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF (Geography = "Urban") ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2023 14:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Referencing-SAS-Macro-Variable-in-Data-Subsetting/m-p/885634#M39295</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-20T14:29:37Z</dc:date>
    </item>
  </channel>
</rss>

