<?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: Insert string variable in datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202235#M37729</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tanks everyone!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 11 Jun 2015 08:23:25 GMT</pubDate>
    <dc:creator>problems99</dc:creator>
    <dc:date>2015-06-11T08:23:25Z</dc:date>
    <item>
      <title>Insert string variable in datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202231#M37725</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a lot of forecasting series (+/- 50), each one in one table. The list of these tables is reported in the variable &lt;STRONG&gt;&lt;EM&gt;code&lt;/EM&gt;&lt;/STRONG&gt; which contains i items.&amp;nbsp; I would like to add a new column &lt;EM&gt;&lt;STRONG&gt;name&lt;/STRONG&gt;&lt;/EM&gt; with the name of the file for each row of each file. (The next step is to merge some rows of some tables and to sort them without losing the origin).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I don't know how I can do that. The red line is my problem. For constant variable, I write that: &lt;EM&gt;&lt;STRONG&gt;name='forecast_01';&lt;/STRONG&gt;&lt;/EM&gt; but here it doesn't seem to be recognized as a string. I tried different things. When it works it creates either an empty column labelled "name" or an empty column labelled with the name of the table but never a column "name" with the name of the table as value for each row as for &lt;EM&gt;&lt;STRONG&gt;name='forecast_01';&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="quote" class="jive_text_macro jive_macro_quote" modifiedtitle="true"&gt;
&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%macro &lt;SPAN style="color: #000000;"&gt;add_name;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%do&lt;/SPAN&gt; i=1 &lt;SPAN style="color: #0000ff;"&gt;%to&lt;/SPAN&gt; &amp;amp;end&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000080;"&gt;data&lt;/SPAN&gt; &amp;amp;&amp;amp;code&amp;amp;i;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;set&lt;/SPAN&gt; &amp;amp;&amp;amp;code&amp;amp;i;&lt;/P&gt;
&lt;P&gt;name=&lt;SPAN style="color: #ff0000;"&gt;%string(&amp;amp;&amp;amp;code&amp;amp;i);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #000080;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;%mend &lt;SPAN style="color: #000000;"&gt;add_name;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: Courier New;"&gt;Thanks for your help!&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jun 2015 14:41:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202231#M37725</guid>
      <dc:creator>problems99</dc:creator>
      <dc:date>2015-06-10T14:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: Insert string variable in datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202232#M37726</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You'll need to use double quotes.&amp;nbsp; Single quotes suppress all macro activity:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;name = "&amp;amp;&amp;amp;code&amp;amp;i";&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Remember macro language isn't changing your data.&amp;nbsp; Macro language is generating the SAS language statements that process the data.&amp;nbsp; You need to be able to picture what the SAS program should look like.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jun 2015 15:07:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202232#M37726</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-06-10T15:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: Insert string variable in datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202233#M37727</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, several methods to get there.&amp;nbsp; If you already have the list of files:&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set list_of_files;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; call execute('data '||strip(filename)||'; set '||strip(filename)||'; name="'||strip(filename)||'"; run;');&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;Or;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;%macro Add_Name (code=);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data &amp;amp;code.;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;code.;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name="&amp;amp;code.";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;%mend Add_name;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;Then call it each element.&amp;nbsp; As I always say, it is easier, and simpler coding to keep you lists in a dataset and generate the code from there.&amp;nbsp; As you are finding with the whole &amp;amp;&amp;amp;code&amp;amp;i. bit, its just not intuitive.&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;A further way is to use in:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;data forecast1 forecast2 ...;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set forecast1 (in=a) forecast2 (in=b)...;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length name $200;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if a then do;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name="forecast1";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output forecast1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;Again, the above can be generated based on &amp;amp;i. and call execute.&amp;nbsp; If all the datasets are the same, then I would suggest setting them altogether and then doing further processing on them.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jun 2015 15:14:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202233#M37727</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-06-10T15:14:03Z</dc:date>
    </item>
    <item>
      <title>Re: Insert string variable in datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202234#M37728</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The option INDSNAME on the set statement will help add a variable to with the set name:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000080;"&gt;data&lt;/SPAN&gt; &amp;amp;&amp;amp;code&amp;amp;i;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff;"&gt;set&lt;/SPAN&gt; &amp;amp;&amp;amp;code&amp;amp;I INDSNAME=dsn;&lt;/P&gt;&lt;P&gt;Name=dsn;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;The DSN is a temporary variable unless assigned to a specific variable during the data set.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;IF the data sets all have the same structure you can combine the date sets AND get the name at one time:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Datat newest;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;lt;list of dataset names&amp;gt; indsname=dsn;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; name=dsn;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And if the input data sets have a similar stem of the name such as Forecast you could use a list shortcut:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;set forecast: indsname=dsn;&lt;/P&gt;&lt;P&gt;name=dsn;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jun 2015 15:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202234#M37728</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2015-06-10T15:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: Insert string variable in datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202235#M37729</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Tanks everyone!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Jun 2015 08:23:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-string-variable-in-datasets/m-p/202235#M37729</guid>
      <dc:creator>problems99</dc:creator>
      <dc:date>2015-06-11T08:23:25Z</dc:date>
    </item>
  </channel>
</rss>

