<?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: symget function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705494#M216445</link>
    <description>Sorry for miss-understanding.&lt;BR /&gt;When we compare the two ways:&lt;BR /&gt;Way1:&lt;BR /&gt;avg_weight = symget(cats('avg_weight_',sex));&lt;BR /&gt;Way2:&lt;BR /&gt;IF sex='M' then avg_weight=&amp;amp;avg_weight_m.; else&lt;BR /&gt;IF sex='F' then avg_weight=&amp;amp;avg_weight_f.;&lt;BR /&gt;&lt;BR /&gt;Why way1 is better than way2????&lt;BR /&gt;</description>
    <pubDate>Sun, 13 Dec 2020 11:25:34 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2020-12-13T11:25:34Z</dc:date>
    <item>
      <title>symget function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705484#M216437</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;May anyone explain the code&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;avg_weight=symget(cats('avg_weight_',sex));&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Why are we using cats here?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;I&amp;nbsp;don't&amp;nbsp;see&amp;nbsp;any&amp;nbsp;concatenating&amp;nbsp;here&amp;nbsp;so&amp;nbsp;why&amp;nbsp;using&amp;nbsp;cats?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;IS&amp;nbsp;it&amp;nbsp;practical&amp;nbsp;code&amp;nbsp;or&amp;nbsp;just&amp;nbsp;in&amp;nbsp;books?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL noprint;
Select avg(case when sex='F' then weight else . end ),
	   avg(case when sex='M' then weight else . end )
into : avg_weight_m trimmed,
	 : avg_weight_f trimmed
from sashelp.class;
quit;

data class_way1;
set sashelp.class;
avg_weight=symget(cats('avg_weight_',sex));
Run;

data class_way2;
set sashelp.class;
IF sex='M' then avg_weight=&amp;amp;avg_weight_m.;
else IF sex='F' then avg_weight=&amp;amp;avg_weight_f.;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Dec 2020 08:11:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705484#M216437</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-13T08:11:00Z</dc:date>
    </item>
    <item>
      <title>Re: symget function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705485#M216438</link>
      <description>&lt;P&gt;Of course there's concatenating done, the string 'avg_weight_' is concatenated with the contents of variable sex. CATS will automatically strip all arguments of leading and trailing blanks.&lt;/P&gt;
&lt;P&gt;The first code shows how to replace a IF/THEN/ELSE with functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code would be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table class as
  select
    cl.*,
    avg(weight) as average_weight
  from sashelp.class cl
  group by sex
  order by name
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Such code does not need to make any assumptions about the possible contents of sex.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 08:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705485#M216438</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-13T08:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: symget function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705492#M216443</link>
      <description>&lt;P&gt;The first bit computes the average weight of each sex and stores the printable representation in a macro variable whose &lt;STRONG&gt;name&lt;/STRONG&gt; contains the sex value.&amp;nbsp; So you have&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;%put &amp;amp;=avg_weight_m;
%put &amp;amp;=avg_weight_f;
---- LOG ----
AVG_WEIGHT_M=90.11111
AVG_WEIGHT_F=108.95
&lt;/LI-CODE&gt;
&lt;P&gt;Storing computation results in the macro symbol table is generally not the best approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Way 1 is just bad coding.&amp;nbsp; Bad awful coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result of&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;cats('avg_weight_', sex)&lt;/PRE&gt;
&lt;P&gt;will be one of&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;'avg_weight_M'&lt;BR /&gt;'avg_weight_F'&lt;/PRE&gt;
&lt;P&gt;which are the names of macro variables.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;avg_weight = symget(cats('avg_weight_',sex));&lt;/LI-CODE&gt;
&lt;P&gt;moves the value of a macro variable to the value of a DATA step variable.&amp;nbsp; However, avg_weight will be $200 because SYMGET returns a character value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Way 2 is not much better.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;IF sex='M' then avg_weight=&amp;amp;avg_weight_m.; else
IF sex='F' then avg_weight=&amp;amp;avg_weight_f.;&lt;/LI-CODE&gt;
&lt;P&gt;After macro resolution becomes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;IF sex='M' then avg_weight=90.11111; else
IF sex='F' then avg_weight=108.95;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case the resolved macro variables are interpreted in the context of DATA Step source code, so the assigned variable ave_weight will take on a numeric type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Typically you want the computation to be made and saved directly (per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;).&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 10:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705492#M216443</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-12-13T10:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: symget function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705494#M216445</link>
      <description>Sorry for miss-understanding.&lt;BR /&gt;When we compare the two ways:&lt;BR /&gt;Way1:&lt;BR /&gt;avg_weight = symget(cats('avg_weight_',sex));&lt;BR /&gt;Way2:&lt;BR /&gt;IF sex='M' then avg_weight=&amp;amp;avg_weight_m.; else&lt;BR /&gt;IF sex='F' then avg_weight=&amp;amp;avg_weight_f.;&lt;BR /&gt;&lt;BR /&gt;Why way1 is better than way2????&lt;BR /&gt;</description>
      <pubDate>Sun, 13 Dec 2020 11:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705494#M216445</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-12-13T11:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: symget function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705495#M216446</link>
      <description>&lt;P&gt;The function call method is more versatile (no literal coding of values needed), but will probably be less performant because of the repeated function calls to retrieve macro variable values, which might come into play when large datasets are processed.&lt;/P&gt;
&lt;P&gt;My SQL is the simplest and easiest to understand code, but might be outperformed by a PROC MEANS and following DATA step with a hash object built from the means. This method avoids any conventional sorting and reads the datasets in sequential order.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 12:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705495#M216446</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-13T12:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: symget function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705497#M216448</link>
      <description>&lt;P&gt;Usually, the only reason to create a data set with the mean by group as a variable is so you can subtract the mean from the actual value to get the difference between the actual value and the mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that's the real reason for doing this, there's no need to create macro variables at all. PROC STDIZE does this subtraction in one step, and would be the simplest to code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12477"&gt;@RichardDeVen&lt;/a&gt;, the two solutions presented are very poor coding.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Dec 2020 12:59:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705497#M216448</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-13T12:59:28Z</dc:date>
    </item>
    <item>
      <title>Re: symget function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705502#M216452</link>
      <description>&lt;P&gt;Your example is poor because you stored numbers into macro variables and because the two methods actually produce different results (a character variable versus a numeric variable) so some respondents are concentrating on that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let just assume the macro variables already exist and concentrate on the final data step that is using the values of those macro variables. The reason the first method using SYMGET() is better than the second is because you do not need to know the number of possible values of the variable being used to generate the macro variable name (SEX in your example) when writing the code.&amp;nbsp; To make it even more robust you could use the SYMEXIST() function to make sure the macro variable actually exists.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let gender_f = Female;
data class_way1;
  set sashelp.class;
  length mvar $32 gender $6 ;
  mvar=cats('gender_',sex);
  if symexist(mvar) then gender=symget(mvar);
run;

proc freq ;
 tables sex*gender / list missing;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                          Cumulative    Cumulative
Sex    gender    Frequency     Percent     Frequency      Percent
------------------------------------------------------------------
F      Female           9       47.37             9        47.37
M                      10       52.63            19       100.00
&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 Dec 2020 15:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/symget-function/m-p/705502#M216452</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-13T15:30:49Z</dc:date>
    </item>
  </channel>
</rss>

