<?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: Do loop assignments in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449561#M113146</link>
    <description>Thanks for the answer. what is the approach do i have to take if the %index is always zero.&lt;BR /&gt;&lt;BR /&gt;I tried to work with data set and i am getting the original values for 1,2,3 diag's.&lt;BR /&gt;&lt;BR /&gt;data fix;&lt;BR /&gt;set test;&lt;BR /&gt;%do i = 1 %to 3;&lt;BR /&gt;%if %index(&amp;amp;(diag_&amp;amp;i.),".") &amp;gt; 0&lt;BR /&gt;%then&lt;BR /&gt;diag_&amp;amp;i. = tranwrd(&amp;amp;(diag_&amp;amp;i.),".","");&lt;BR /&gt;%put &amp;amp;=diag_&amp;amp;i.;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;</description>
    <pubDate>Thu, 29 Mar 2018 03:27:49 GMT</pubDate>
    <dc:creator>SASAna</dc:creator>
    <dc:date>2018-03-29T03:27:49Z</dc:date>
    <item>
      <title>Do loop assignments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449554#M113141</link>
      <description>&lt;P&gt;Hello SAS users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need&amp;nbsp; help with tuning this query.&lt;/P&gt;&lt;P&gt;Read the diag codes , if decimals in the data, replace the dots&amp;nbsp; (5.1 should become 5) if no decimal present retain the data as it is. I am not getting any values in put statements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro test1;&lt;BR /&gt;data test;&lt;BR /&gt;&lt;BR /&gt;diag_1 = 5.1;&lt;BR /&gt;diag_2 = 4;&lt;BR /&gt;diag_3 = 4.7;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%do i = 1 %to 3;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %if %index(diag_&amp;amp;i,".") &amp;gt; 0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; diag_&amp;amp;i = tranwrd(diag_&amp;amp;i,".","");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; diag_&amp;amp;i;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; %put &amp;amp;diag_&amp;amp;i;&lt;BR /&gt;&amp;nbsp;%END;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;%mend;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2018 02:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449554#M113141</guid>
      <dc:creator>SASAna</dc:creator>
      <dc:date>2018-03-29T02:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop assignments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449558#M113143</link>
      <description>&lt;P&gt;First figure out how to write the code using SAS.&amp;nbsp; Then you if there is some parts that need to change systematically&amp;nbsp; or there is repetitive code that you would like to generate you can look into trying to use macro coding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now your %IF condition will always be false because the %INDEX() function will always return zero.&amp;nbsp; There are no periods in either the string &lt;STRONG&gt;diag_&lt;/STRONG&gt; or the value of the macro variable I since the macro variable will only every have integer values.&amp;nbsp; Plus you are trying to use the macro to generate assignment statements that can only work in a data step, but you did not call the macro inside of a data step so any assignment statement it generates will just cause syntax errors for SAS.&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, 29 Mar 2018 02:41:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449558#M113143</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-29T02:41:40Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop assignments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449561#M113146</link>
      <description>Thanks for the answer. what is the approach do i have to take if the %index is always zero.&lt;BR /&gt;&lt;BR /&gt;I tried to work with data set and i am getting the original values for 1,2,3 diag's.&lt;BR /&gt;&lt;BR /&gt;data fix;&lt;BR /&gt;set test;&lt;BR /&gt;%do i = 1 %to 3;&lt;BR /&gt;%if %index(&amp;amp;(diag_&amp;amp;i.),".") &amp;gt; 0&lt;BR /&gt;%then&lt;BR /&gt;diag_&amp;amp;i. = tranwrd(&amp;amp;(diag_&amp;amp;i.),".","");&lt;BR /&gt;%put &amp;amp;=diag_&amp;amp;i.;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;</description>
      <pubDate>Thu, 29 Mar 2018 03:27:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449561#M113146</guid>
      <dc:creator>SASAna</dc:creator>
      <dc:date>2018-03-29T03:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop assignments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449564#M113149</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86567"&gt;@SASAna&lt;/a&gt; wrote:&lt;BR /&gt;Thanks for the answer. what is the approach do i have to take if the %index is always zero.&lt;BR /&gt;&lt;BR /&gt;I tried to work with data set and i am getting the original values for 1,2,3 diag's.&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fix;
set test;
%do i = 1 %to 3;
%if %index(&amp;amp;(diag_&amp;amp;i.),".") &amp;gt; 0
%then
diag_&amp;amp;i. = tranwrd(&amp;amp;(diag_&amp;amp;i.),".","");
%put &amp;amp;=diag_&amp;amp;i.;
%end;
run&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BR /&gt;&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I thought you&amp;nbsp;had the data in dataset variables?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to operate on a series of variables you should learn how to use the ARRAY statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also if you want to use string functions like INDEX() or TRANWRD() make sure that you have defined your variables as character variables and not numeric variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make a sample dataset that has three character variables named DIAG1 to DIAG3&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  length diag1-diag3 $10 ;
  input diag1-diag3;
cards;
5.1 4 4.7
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And write a program to remove the periods from the values of those variables.&lt;/P&gt;
&lt;P&gt;Here is how you could do it with an ARRAY statement and a DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set test;
  array dx diag1-diag3;
  do i=1 to dim(dx);
     dx(i)=compress(dx(i),'.');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to use macro code to generate a series of statements then make sure the statements you are generating are valid SAS statements.&amp;nbsp; For example instead of using an ARRAY you could just type out a separate statement for each of the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set test;
  diag1= compress(diag1,'.');
  diag2= compress(diag2,'.');
  diag3= compress(diag3,'.');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now that you have some code that can run you could try to construct a macro&amp;nbsp;that will generate the repetitive statements for you.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
data want ;
  set test;
%do i=1 %to 3;
  diag&amp;amp;i = compress(diag&amp;amp;i,'.');
%end;
run;
%mend ;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2018 03:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449564#M113149</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-29T03:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop assignments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449566#M113150</link>
      <description>Thanks Tom. I used the macro method and it worked very well.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 29 Mar 2018 03:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449566#M113150</guid>
      <dc:creator>SASAna</dc:creator>
      <dc:date>2018-03-29T03:53:54Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop assignments</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449567#M113151</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/86567"&gt;@SASAna&lt;/a&gt; wrote:&lt;BR /&gt;Thanks Tom. I used the macro method and it worked very well.&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You should wait to start using macros until after you understand how to write SAS code.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Mar 2018 03:55:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop-assignments/m-p/449567#M113151</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-29T03:55:14Z</dc:date>
    </item>
  </channel>
</rss>

