<?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: why &amp;amp;i=trtn is false, how to modify the code in %do %end in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617698#M181049</link>
    <description>&lt;P&gt;You do not need a macro at all, as proper lookup technique is done with data step means only:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input trtn siteid $ f1;
cards;
1 A 10
2 A 15
3 A 25
1 B 10
2 B 15
3 B 25
1 C 10
2 C 15
3 C 25
;

data lookup;
input trtn n;
datalines;
1 100
2 200
3 300
;

data pct;
length npct $200;
set have;
if _n_ = 1
then do;
  declare hash lookup (dataset:"lookup");
  lookup.definekey('trtn');
  lookup.definedata('n');
  lookup.definedone();
  n = 0; * prevents "uninitialized" NOTE;
end;
if lookup.find() = 0 then npct=strip(put(f1,best.))||" ("||strip(put(f1/n*100,5.1))||")";
drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In place of the hash, you could also use a join or an informat created from the dataset, but the hash is the way to go for issues like this.&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2020 07:09:53 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-01-16T07:09:53Z</dc:date>
    <item>
      <title>why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617684#M181040</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="2.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35379i0AEABA5B74DB2057/image-size/large?v=v2&amp;amp;px=999" role="button" title="2.JPG" alt="2.JPG" /&gt;&lt;/span&gt;***total number for each siteid
A  100
B  200
C  300
;

data have;
input trtn siteid $ f1 ;
cards;
1 A 10
2 A 15
3 A 25
1 B 10
2 B 15
3 B 25
1 C 10
2 C 15
3 C 25
;
run;

%let n1=100;
%let n2=200;
%let n3=300;

data pct;
	length npct $200;
	set have;
	if trtn=1 then npct=strip(put(f1,best.))||" ("||strip(put(f1/&amp;amp;n1*100,5.1))||")";
	if trtn=2 then npct=strip(put(f1,best.))||" ("||strip(put(f1/&amp;amp;n2*100,5.1))||")";
	if trtn=3 then npct=strip(put(f1,best.))||" ("||strip(put(f1/&amp;amp;n3*100,5.1))||")";
run;

option symbolgen mprint mlogic;
%MACRO PCT;
DATA pct;
	LENGTH NPCT $200;
	SET have;
	%*why &amp;amp;i=trtn is false, how to modify the code??;
	%DO i=1 %TO 3;
		%if &amp;amp;i=trtn %then %do;
			n=&amp;amp;&amp;amp;n&amp;amp;i;
			if f1&amp;gt;. and n&amp;gt;. then NPCT=strip(put(F1,best.))||" ("||strip(put(F1/n*100,5.1))||")";
		%end;
	%END;
RUN;
%MEND;
%PCT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;the result in the data step is what I want, but the macro %pct cannot work, why? how?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 06:09:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617684#M181040</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-16T06:09:39Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617686#M181041</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35380iDA9FAC089DDE941F/image-size/large?v=v2&amp;amp;px=999" role="button" title="2.JPG" alt="2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 06:11:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617686#M181041</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-16T06:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617688#M181043</link>
      <description>&lt;P&gt;&lt;SPAN class="token operator"&gt;Sorry for my mistake,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token operator"&gt;updated as below:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token operator"&gt;**&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;total number for each trtn&lt;/P&gt;&lt;P&gt;trtn total&lt;BR /&gt;1&amp;nbsp;&amp;nbsp; &lt;SPAN class="token number"&gt;100&lt;/SPAN&gt;&lt;BR /&gt;2&amp;nbsp;&amp;nbsp; &lt;SPAN class="token number"&gt;200&lt;/SPAN&gt;&lt;BR /&gt;3 &amp;nbsp; &lt;SPAN class="token number"&gt;300&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 06:18:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617688#M181043</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-16T06:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617689#M181044</link>
      <description>&lt;P&gt;Because you are comparing the value of macro variable i with the string trtn - which is always false, because of your definition of i. Why do you use macro-statements at all?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 06:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617689#M181044</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-01-16T06:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617698#M181049</link>
      <description>&lt;P&gt;You do not need a macro at all, as proper lookup technique is done with data step means only:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input trtn siteid $ f1;
cards;
1 A 10
2 A 15
3 A 25
1 B 10
2 B 15
3 B 25
1 C 10
2 C 15
3 C 25
;

data lookup;
input trtn n;
datalines;
1 100
2 200
3 300
;

data pct;
length npct $200;
set have;
if _n_ = 1
then do;
  declare hash lookup (dataset:"lookup");
  lookup.definekey('trtn');
  lookup.definedata('n');
  lookup.definedone();
  n = 0; * prevents "uninitialized" NOTE;
end;
if lookup.find() = 0 then npct=strip(put(f1,best.))||" ("||strip(put(f1/n*100,5.1))||")";
drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In place of the hash, you could also use a join or an informat created from the dataset, but the hash is the way to go for issues like this.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 07:09:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/617698#M181049</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-01-16T07:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618005#M181219</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt; , thank you for correcting my mistake, actually I want compare the value of &amp;amp;i with the value of variable trtn; is there any function to get the value of variable?</description>
      <pubDate>Fri, 17 Jan 2020 05:40:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618005#M181219</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-17T05:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618007#M181220</link>
      <description>thank yop, KurtBremser , it is a good idea to use HASH to merge with trtn, but I intent to get the value of variable (trtn) which is equal to the value of macro variable(&amp;amp;i)</description>
      <pubDate>Fri, 17 Jan 2020 05:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618007#M181220</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-17T05:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618016#M181224</link>
      <description>&lt;P&gt;Please post the expected result you want out of your example data.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 07:29:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618016#M181224</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-01-17T07:29:59Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618060#M181248</link>
      <description>&lt;P&gt;Sounds like you want to compare the value of the data step variable TRTN to a literal value that happens to come from resolving &amp;amp;i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you want to evaluate the value of a data step variable, you use a data step IF statement, not a macro %IF statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thus if you change your code to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO PCT;&lt;BR /&gt;&lt;BR /&gt;%local i;&lt;BR /&gt;
DATA pct;
	LENGTH NPCT $200;
	SET have;

	%DO i=1 %TO 3;
		if &amp;amp;i=trtn then do;
			n=&amp;amp;&amp;amp;n&amp;amp;i;
			if f1&amp;gt;. and n&amp;gt;. then NPCT=strip(put(F1,best.))||" ("||strip(put(F1/n*100,5.1))||")";
		end;
	%END;
RUN;
%MEND;
%PCT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code works.&amp;nbsp; Note that the macro is simply generating SAS code.&amp;nbsp; The above macro will generate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA pct;
	LENGTH NPCT $200;
	SET have;

		if 1=trtn then do;
			n=100;
			if f1&amp;gt;. and n&amp;gt;. then NPCT=strip(put(F1,best.))||" ("||strip(put(F1/n*100,5.1))||")";
		end;

		if 2=trtn then do;
			n=200;
			if f1&amp;gt;. and n&amp;gt;. then NPCT=strip(put(F1,best.))||" ("||strip(put(F1/n*100,5.1))||")";
		end;

		if 3=trtn then do;
			n=300;
			if f1&amp;gt;. and n&amp;gt;. then NPCT=strip(put(F1,best.))||" ("||strip(put(F1/n*100,5.1))||")";
		end;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Looking at that step showing the generated code, there are likely better ways in SAS to write this sort of lookup.&amp;nbsp; But the code is valid syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 12:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618060#M181248</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-01-17T12:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618101#M181272</link>
      <description>&lt;P&gt;It doesn't work because the value of &amp;amp;I can only be the digit strings 1 ,&amp;nbsp; 2 or 3 and none of those will ever equal the string trtn.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to use a %DO loop to recreate the repetitive code in your first step then there is no need for and %IF statements.&lt;/P&gt;
&lt;P&gt;Just replace the digit 1 with &amp;amp;i in the first example. Make sure to add an extra &amp;amp; so that you can get the value of macro variable N1 instead of the value of macro variable N.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data pct;
  length npct $200;
  set have;
%do i=1 %to 3 ;
  if trtn=&amp;amp;i then npct=strip(put(f1,best.))||" ("||strip(put(f1/&amp;amp;&amp;amp;n&amp;amp;i*100,5.1))||")";
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or just eliminate the need for any looping by using the SYMGETN() function instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data pct;
  length npct $200;
  set have;
  npct=strip(put(f1,best.))||" ("||strip(put(f1/symgetn(cats('N',trtn))*100,5.1))||")";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Jan 2020 15:40:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618101#M181272</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-01-17T15:40:26Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618458#M181443</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, thanks a lot, I prefer to use funtion symgetn.</description>
      <pubDate>Mon, 20 Jan 2020 05:42:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618458#M181443</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-20T05:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618459#M181444</link>
      <description>Hi KurtBremser , the answers from andreas_lds and Tom is what I really want, thanks anyway.</description>
      <pubDate>Mon, 20 Jan 2020 05:44:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618459#M181444</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-20T05:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: why &amp;i=trtn is false, how to modify the code in %do %end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618460#M181445</link>
      <description>@ Quentin , thanks your post, Tom's answer has resolved my question.</description>
      <pubDate>Mon, 20 Jan 2020 05:47:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/why-amp-i-trtn-is-false-how-to-modify-the-code-in-do-end/m-p/618460#M181445</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2020-01-20T05:47:29Z</dc:date>
    </item>
  </channel>
</rss>

