<?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: [macro] Used Lenght in loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535811#M147171</link>
    <description>&lt;P&gt;If you want to understand yout own code, you have to write it in a way that makes it easy to read and understand:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tt();

data  _null_;
set test;
call symputx('nbr', _n_);
run;

%put nbr=&amp;amp;nbr.;
%do j = 1 %to &amp;amp;nbr.;

  data _null_;
  set test(obs=&amp;amp;j.);
  call symputx('size', length(var));
  run;

  data test1;
  set test(obs=&amp;amp;j.);&lt;BR /&gt;
  %do i = 1 %to &amp;amp;size.;
    %put i=&amp;amp;i;&lt;BR /&gt;
    lettre = substr(var,&amp;amp;i.,1);
    type = indexc(lettre,'3A3B3C'x);
    if type 
    then do;
      substr(var,&amp;amp;i.,1)=translate(lettre,'414243'x,'3A3B3C'x);
    end;
    else do;
      substr(var,&amp;amp;i.,1)=lettre; 
    end;&lt;BR /&gt;
  %end;    
%end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Hint: do NOT use tabs in code; different systems and users have different tab settings, which skewer the display of code. Replace tabs with blanks, EG has a setting for that)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most of your problems come from a misunderstanding what the obs= dataset option does. It does not read a single observation, but &lt;EM&gt;all&lt;/EM&gt; observations up to the given number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also &lt;EM&gt;repeatedly overwrite&lt;/EM&gt; test1 in the outer macro loop, so only the results of the last outer loop iteration will take effect. This last iteration is run with the length of the variable in the last observation of test. Since that is the shorter one, you have an incomplete execution of the translate for the first observation that is dealt with in the same step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To determine the number of observations in a dataset, your method is inefficient:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set test;
call symputx('nbr', _n_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The whole dataset is read, and for every observation, a call symputx is made. If test is empty, no macro variable is created, and subsequent calls to it will fail. Do this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  _null_;
call symputx('nbr', nobs);
stop;
set test nobs=nobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A temporary variable nobs is created by the option of the set statement, and the data step stops immediately after the call symputx, without reading even one observation. OTOH, even with zero observations in the dataset, a correct value is set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bottom line: you still have a very rudimentary understanding of Base SAS code and the data step, so you are WAY out of your depth with dabbling in macro programming. Learn the basics of data step programming first before advancing into macro processing, and DO NOT abuse the macro preprocessor for data handling (which is what you are doing here). You will NOT learn anything useful that way, but only waste your time and ours.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Feb 2019 07:38:04 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-02-15T07:38:04Z</dc:date>
    <item>
      <title>[macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535170#M146932</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;I want to change the special characters using this code.&lt;BR /&gt;It's remain to define the size of observation.&lt;BR /&gt;the function length does not work&lt;BR /&gt;could you, please, help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tt();
data test1;
set test;

%do i=1 %to %sysfunc(length(var));
        lettre=substr(var,&amp;amp;i.,1);
        type=indexc(lettre,'3A3B3C'x);
	if type then do; substr(var,&amp;amp;i.,1)=translate(lettre,'414243'x,'3A3B3C'x);
	end;
	else do; substr(var,&amp;amp;i.,1)=lettre; 
	end;
 %end;    
 run;
%mend;
%tt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Whith this method, the code work correctely&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
set test;
var1=translate(var,'ABC','3A3B3C'x);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;NB: these are tests that I do for myself&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 12:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535170#M146932</guid>
      <dc:creator>mansour_ib_sas</dc:creator>
      <dc:date>2019-02-13T12:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535175#M146934</link>
      <description>&lt;P&gt;Use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic symbolgen;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;before executing the macro, read the log after execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%sysfunc(length(var))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will always return 3, because you don't get the length of the contents of "var", but the length of the text "var".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=mcrolref&amp;amp;docsetTarget=titlepage.htm&amp;amp;locale=de" target="_self"&gt;docs&lt;/A&gt; have an interesting chapter about macros.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 13:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535175#M146934</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-02-13T13:05:31Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535179#M146936</link>
      <description>&lt;P&gt;"var" is NOT a macro variable, but probably a data step variable. Its contents are NOT available for macro processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the umpteenth time:&lt;/P&gt;
&lt;P&gt;The macro &lt;FONT size="5"&gt;&lt;STRONG&gt;PRE&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT size="2"&gt;processor is for generating dynamic code, &lt;STRONG&gt;&lt;FONT size="5"&gt;NOT&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT size="5"&gt;&lt;FONT size="2"&gt; for handling data.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 13:13:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535179#M146936</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-13T13:13:52Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535180#M146937</link>
      <description>&lt;P&gt;What are you trying to accomplish?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have characters in column&amp;nbsp;&lt;STRONG&gt;Var&amp;nbsp;&lt;/STRONG&gt;that you want to remove?&lt;/P&gt;
&lt;P&gt;If yes, you don't need a macro for that, just keep your data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want to translate the individual characters, ie. one character at a time? then do this instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var1 = translate(char,'A','3A'x, 'B','3B'x, 'C','3C'x);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just remember that the macro executes&amp;nbsp;&lt;STRONG&gt;before&lt;/STRONG&gt; the data step starts, think of a macro as something that generate code statements.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 13:14:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535180#M146937</guid>
      <dc:creator>MichaelLarsen</dc:creator>
      <dc:date>2019-02-13T13:14:48Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535185#M146939</link>
      <description>&lt;P&gt;If VAR is indeed a data step variable, DO NOT use macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to %sysfunc(length(var));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should be a data step DO loop&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to length(var);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But, we here don't really know what VAR is, you haven't told us.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note: it is not a good idea to mix and match data step and macro code. While it can be done, you have to be absolutely sure you are performing data step functions on data step variables and macro functions on macro variables; and it should only be done when the data step by itself won't handle the situation without macros.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 13:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535185#M146939</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-13T13:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535609#M147103</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I almost arrived with my solution.&lt;BR /&gt;for use correctly the length and the size of each observation, it was necessary to browse one by one.&lt;BR /&gt;I say almost because, when the observation size 1 of the variable var is greater than the size of the observation 2, the result in the new table test1 are same size and the processing of the observation 1 is "interrupted" and I do not understand why?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
length var $10.;
var='test;;:ii&amp;lt;';
output;
var='testj;;';
output;
run;


option symbolgen mprint mlogic;

%macro tt();

data  _null_;
set test;
call symputx('nbr', _n_);
run;

%put nbr=&amp;amp;nbr.;
%do j=1 %to &amp;amp;nbr.;


data _null_;
set test(obs=&amp;amp;j.);
call symputx('size', length(var));
run;

data test1;
set  test(obs=&amp;amp;j.);

%do i=1 %to &amp;amp;size.;

%put i=&amp;amp;i;

        lettre=substr(var,&amp;amp;i.,1);
        type  =indexc(lettre,'3A3B3C'x);
       
        if type 
	    then do; substr(var,&amp;amp;i.,1)=translate(lettre,'414243'x,'3A3B3C'x);
	end;
	
        else do; substr(var,&amp;amp;i.,1)=lettre; 
        end;
%end;    
%end;
run;
%mend;

%tt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 14 Feb 2019 15:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535609#M147103</guid>
      <dc:creator>mansour_ib_sas</dc:creator>
      <dc:date>2019-02-14T15:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535649#M147114</link>
      <description>&lt;P&gt;Hi mansour_ib_sas&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It will be a great help if you can give us an example of your wanted output. what should the output in test1 look like? - It seems you are trying to accomplish a relative simple task in a very complicated way that does not utilize the power of SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From your example it seems that you examine each single character and translate ; to A, ; to B and &amp;lt; to C. If that is what you want, then the following code will do the job:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test1; set test;
	var = translate(var,'414243'x,'3A3B3C'x);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;testBBAiiC&lt;/P&gt;
&lt;P&gt;testjBB&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;
&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, 14 Feb 2019 17:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535649#M147114</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-14T17:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535811#M147171</link>
      <description>&lt;P&gt;If you want to understand yout own code, you have to write it in a way that makes it easy to read and understand:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tt();

data  _null_;
set test;
call symputx('nbr', _n_);
run;

%put nbr=&amp;amp;nbr.;
%do j = 1 %to &amp;amp;nbr.;

  data _null_;
  set test(obs=&amp;amp;j.);
  call symputx('size', length(var));
  run;

  data test1;
  set test(obs=&amp;amp;j.);&lt;BR /&gt;
  %do i = 1 %to &amp;amp;size.;
    %put i=&amp;amp;i;&lt;BR /&gt;
    lettre = substr(var,&amp;amp;i.,1);
    type = indexc(lettre,'3A3B3C'x);
    if type 
    then do;
      substr(var,&amp;amp;i.,1)=translate(lettre,'414243'x,'3A3B3C'x);
    end;
    else do;
      substr(var,&amp;amp;i.,1)=lettre; 
    end;&lt;BR /&gt;
  %end;    
%end;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Hint: do NOT use tabs in code; different systems and users have different tab settings, which skewer the display of code. Replace tabs with blanks, EG has a setting for that)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most of your problems come from a misunderstanding what the obs= dataset option does. It does not read a single observation, but &lt;EM&gt;all&lt;/EM&gt; observations up to the given number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also &lt;EM&gt;repeatedly overwrite&lt;/EM&gt; test1 in the outer macro loop, so only the results of the last outer loop iteration will take effect. This last iteration is run with the length of the variable in the last observation of test. Since that is the shorter one, you have an incomplete execution of the translate for the first observation that is dealt with in the same step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To determine the number of observations in a dataset, your method is inefficient:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set test;
call symputx('nbr', _n_);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The whole dataset is read, and for every observation, a call symputx is made. If test is empty, no macro variable is created, and subsequent calls to it will fail. Do this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data  _null_;
call symputx('nbr', nobs);
stop;
set test nobs=nobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A temporary variable nobs is created by the option of the set statement, and the data step stops immediately after the call symputx, without reading even one observation. OTOH, even with zero observations in the dataset, a correct value is set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bottom line: you still have a very rudimentary understanding of Base SAS code and the data step, so you are WAY out of your depth with dabbling in macro programming. Learn the basics of data step programming first before advancing into macro processing, and DO NOT abuse the macro preprocessor for data handling (which is what you are doing here). You will NOT learn anything useful that way, but only waste your time and ours.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Feb 2019 07:38:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535811#M147171</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-15T07:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535812#M147172</link>
      <description>&lt;P&gt;Note that your &lt;EM&gt;whole&lt;/EM&gt; "experimental" logic can be solved without &lt;EM&gt;any&lt;/EM&gt; macro code at all in one simple data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
length var $10.;
var = 'test;;:ii&amp;lt;';
output;
var = 'testj;;';
output;
run;

data test1;
set test;
do i = 1 to length(var);
  lettre = substr(var,i,1);
  type = indexc(lettre,'3A3B3C'x);
  if type
  then do;
    substr(var,i,1)=translate(lettre,'414243'x,'3A3B3C'x);
  end;
  else do;
    substr(var,i,1) = lettre; 
  end;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Feb 2019 07:43:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/535812#M147172</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-15T07:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: [macro] Used Lenght in loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/538285#M148168</link>
      <description>&lt;P&gt;Thank You,&lt;/P&gt;&lt;P&gt;Just whith data step, the code work.&lt;/P&gt;&lt;P&gt;I note&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sincerely&lt;/P&gt;</description>
      <pubDate>Mon, 25 Feb 2019 15:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-Used-Lenght-in-loop/m-p/538285#M148168</guid>
      <dc:creator>mansour_ib_sas</dc:creator>
      <dc:date>2019-02-25T15:00:25Z</dc:date>
    </item>
  </channel>
</rss>

