<?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: A question on using index as a macro function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138505#M27956</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks a lot, Tom. Yeah, your code is definitely the correct way to do that and mine is messy. I guess I need to get more familiar with data step!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 28 Jul 2014 20:02:01 GMT</pubDate>
    <dc:creator>Shayan2012</dc:creator>
    <dc:date>2014-07-28T20:02:01Z</dc:date>
    <item>
      <title>A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138499#M27950</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am a bit confused about using index function since I am not getting the desired result.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, I have written a Macro, which first assigns a name to some macro variable, and then searches for that variable in a text, as the code below. My problem is that I get y=0 everytime that I search for the text, even though that I know that the desired text is in the source. Can some one please tell me what I am doing wrong?&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data list;&lt;/P&gt;&lt;P&gt;input target $;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;BILL&lt;/P&gt;&lt;P&gt;JOE&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data source;&lt;/P&gt;&lt;P&gt;input line $;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;TESTBILL&lt;/P&gt;&lt;P&gt;Aaa&lt;/P&gt;&lt;P&gt;JOEY&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro test;&lt;/P&gt;&lt;P&gt;%DO d=1 %TO 2 ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; data Listfile;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do d=&amp;amp;d;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set list point=d;&lt;/P&gt;&lt;P&gt;&amp;nbsp; CALL SYMPUT("NAMEVAR",target);&lt;/P&gt;&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; stop;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;%put &amp;amp;NAMEVAR;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %do b=1 %to 3;&lt;/P&gt;&lt;P&gt;&amp;nbsp; data crawler;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do b=&amp;amp;b;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set source point=b;&lt;/P&gt;&lt;P&gt;&amp;nbsp; y=index(line,"&amp;amp;NAMEVAR");&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; stop;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp; %end;&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%test;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 Jul 2014 17:56:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138499#M27950</guid>
      <dc:creator>Shayan2012</dc:creator>
      <dc:date>2014-07-27T17:56:25Z</dc:date>
    </item>
    <item>
      <title>Re: Use macrofunction or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138500#M27951</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;CALL SYMPUT() puts a value into a symbol&lt;/P&gt;&lt;P&gt;From where will your data step obtain its value in NAME?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 Jul 2014 18:24:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138500#M27951</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2014-07-27T18:24:29Z</dc:date>
    </item>
    <item>
      <title>Re: Use macrofunction or not?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138501#M27952</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am sorry Peter. My question was submitted incomplete so I was editing it. I just edited that in the main question. So, I am trying to read the variable target from the list dataset and put it in the NAMEVAR macro variable.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 Jul 2014 18:42:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138501#M27952</guid>
      <dc:creator>Shayan2012</dc:creator>
      <dc:date>2014-07-27T18:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138502#M27953</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your program does not make any sense, but if you put this line after the line that is calculating Y you should be able to see that the trailing spaces you put into the macro variable by using CALL SYMPUT instead of CALL SYMPUTX is probably why you are not getting any hits.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 12pt;"&gt;&amp;nbsp; put y= line= "NAMEVAR='&amp;amp;namevar'";&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 Jul 2014 22:10:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138502#M27953</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2014-07-27T22:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138503#M27954</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If you want to compare all lines to all targets you do not need to mess with macro coding.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;data want ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&amp;nbsp; set source ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&amp;nbsp; do p=1 to nobs ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set list point=p nobs=nobs ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; y=index(line,trim(target));&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; put (_n_ p line target y) (=) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasNote" id="sasLogNote5_1406499290561" style="color: #0000ff;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasNote" style="color: #0000ff;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt; color: #000000;"&gt;_N_=1 p=1 line=TESTBILL target=BILL y=5&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;_N_=1 p=2 line=TESTBILL target=JOE y=0&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;_N_=2 p=1 line=Aaa target=BILL y=0&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;_N_=2 p=2 line=Aaa target=JOE y=0&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;_N_=3 p=1 line=JOEY target=BILL y=0&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasSource" style="color: #000000;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;_N_=3 p=2 line=JOEY target=JOE y=1&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasNote" id="sasLogNote6_1406499290561" style="color: #0000ff;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasNote" style="color: #0000ff;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;NOTE: There were 3 observations read from the data set WORK.SOURCE.&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="sasNote" id="sasLogNote7_1406499290561" style="color: #0000ff;"&gt;&lt;SPAN style="font-family: 'courier new', courier; font-size: 10pt;"&gt;NOTE: The data set WORK.WANT has 6 observations and 3 variables.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 27 Jul 2014 22:17:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138503#M27954</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2014-07-27T22:17:08Z</dc:date>
    </item>
    <item>
      <title>Re: A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138504#M27955</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It is good to comment on the macro code, to better understand how it works... &lt;/P&gt;&lt;P&gt;Use options mprint mlogic to better understand what is going on when the macro is executed... &lt;/P&gt;&lt;P&gt;First,&amp;nbsp; you have to trim trailing spaces from the test string :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #575757;"&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt;CALL SYMPUT ("NAMEVAR"&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt;,trim(target));&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The dataset crawler remains empty because there is nothing written to the dataset...&lt;/P&gt;&lt;P&gt;An output statement before the stop statement would write a line to the crawler dataset... &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; data crawler;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; do b=&amp;amp;b;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; set source point=b;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; y=index(line,"&amp;amp;NAMEVAR");&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; stop;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The datastep generated by the macro also is generated at every loop .... when executing one after the other, the same dataset is recreated at every datastep boundary.. so you would only see the last result ... &lt;/P&gt;&lt;P&gt;When for example using the counters in the dataset name :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="font-family: courier new,courier;"&gt; data crawler&amp;amp;b&amp;amp;d;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;a dataset is generated for every iteration..&lt;/P&gt;&lt;P&gt;showing the results....&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jul 2014 14:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138504#M27955</guid>
      <dc:creator>wimaerts</dc:creator>
      <dc:date>2014-07-28T14:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138505#M27956</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks a lot, Tom. Yeah, your code is definitely the correct way to do that and mine is messy. I guess I need to get more familiar with data step!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jul 2014 20:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138505#M27956</guid>
      <dc:creator>Shayan2012</dc:creator>
      <dc:date>2014-07-28T20:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138506#M27957</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks a lot wimaerts. Actually, I don't think that trim be the problem. Since I had already take care of spaces in the previous steps of my code. Thats why I thought I am misusing macro functions in some way. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Jul 2014 20:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138506#M27957</guid>
      <dc:creator>Shayan2012</dc:creator>
      <dc:date>2014-07-28T20:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138507#M27958</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;SAS stores character variables as fixed length strings.&amp;nbsp; So if variable target is of length $8 when you store the value 'BILL' into target it is actually stored with 4 trailing spaces to pad the field out to its full length.&amp;nbsp; So when you use CALL SYMPUT('namevar',target) you are generating macro variable NAMEVAR with 4 extra spaces.&amp;nbsp; If you use call symputX('namevar',target) then the trailing spaces are not put into the macro variable.&amp;nbsp; Or you can do it yourself with the TRIM() function and continue to use CALL SYMPUT.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Jul 2014 00:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138507#M27958</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2014-07-29T00:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: A question on using index as a macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138508#M27959</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could also use SQL for this task.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PROC SQL NOPRINT;&lt;/P&gt;&lt;P&gt;CREATE TABLE WANT1 AS&lt;/P&gt;&lt;P&gt;SELECT *,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INDEX(LINE,TRIM(TARGET)) AS POS&lt;/P&gt;&lt;P&gt;FROM LIST A,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SOURCE B&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Jul 2014 01:39:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-question-on-using-index-as-a-macro-function/m-p/138508#M27959</guid>
      <dc:creator>Scott_Mitchell</dc:creator>
      <dc:date>2014-07-29T01:39:08Z</dc:date>
    </item>
  </channel>
</rss>

