<?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: Add in new variable to dataset, with same 'string' for every line in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758719#M239622</link>
    <description>&lt;P&gt;You should read the documentation for the INFILE statement as there are lots of options. In this case you want Filename.&lt;/P&gt;
&lt;PRE&gt;data sas_1.test ;
infile "c:\2\gt\i_21503a.txt" filename=tempfile;
length myfilevar $ 200;
myfilevar = tempfile; 
format Indicator $30. ;&lt;BR /&gt;&amp;lt;rest of data step to read data&amp;gt;&lt;/PRE&gt;
&lt;P&gt;The option creates a temporary variable, one that will not get written to the data set. So you have to assign that value to the variable you want. And since this is character it is a good idea to take control of the length of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that if you are reading multiple files, such as with a wildcard character or explicit filename that points to multiple files the variable with change as the new files are read.&lt;/P&gt;</description>
    <pubDate>Mon, 02 Aug 2021 07:34:25 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-08-02T07:34:25Z</dc:date>
    <item>
      <title>Add in new variable to dataset, with same 'string' for every line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758712#M239615</link>
      <description>&lt;P&gt;A few days ago I made it this far, in creating a particular dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sas_1.test ;
infile "c:\2\gt\i_21503a.txt";
format Indicator $30. ;
format Parameter 12.2 ;
format Number 12.0 ;
format Success 12.2 ;
format Fail 12.2 ;
Input
Indicator $
Parameter
Number
Success
Fail;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code above creates just one of &lt;STRONG&gt;many&lt;/STRONG&gt; such datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each of the datasets, I would like to include an additional variable that represents, basically, the 'path' to the infile .txt file -- the initial data source.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's call the new variable "Path".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, in the case above, the "Path" variable would contain:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2_gt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Every line of data in the dataset would additionally include this "Path" variable (I would rearrange it to be the very first variable), with the same element all the way down, "2_gt".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another dataset might have "Path", with all lines being "3_xy".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the question is, how to enter a new variable, with the same thing all the way down.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Too, can I include doing so in the code at top?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suggestions greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nicholas Kormanik&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>Mon, 02 Aug 2021 06:56:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758712#M239615</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2021-08-02T06:56:55Z</dc:date>
    </item>
    <item>
      <title>Re: Add in new variable to dataset, with same 'string' for every line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758719#M239622</link>
      <description>&lt;P&gt;You should read the documentation for the INFILE statement as there are lots of options. In this case you want Filename.&lt;/P&gt;
&lt;PRE&gt;data sas_1.test ;
infile "c:\2\gt\i_21503a.txt" filename=tempfile;
length myfilevar $ 200;
myfilevar = tempfile; 
format Indicator $30. ;&lt;BR /&gt;&amp;lt;rest of data step to read data&amp;gt;&lt;/PRE&gt;
&lt;P&gt;The option creates a temporary variable, one that will not get written to the data set. So you have to assign that value to the variable you want. And since this is character it is a good idea to take control of the length of the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that if you are reading multiple files, such as with a wildcard character or explicit filename that points to multiple files the variable with change as the new files are read.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 07:34:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758719#M239622</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-08-02T07:34:25Z</dc:date>
    </item>
    <item>
      <title>Re: Add in new variable to dataset, with same 'string' for every line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758721#M239624</link>
      <description>&lt;P&gt;Untested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* add length after data-line */
length path $ 4 _filename $ 400;
 
/* change infile-statement */
infile "c:\2\gt\i_21503a.txt" filename=_filename;
/* .... */

path = cats(scan(_filename, 2, '\'), '_', scan(_filename, 3, '\'));

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Aug 2021 07:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758721#M239624</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-08-02T07:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Add in new variable to dataset, with same 'string' for every line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758725#M239627</link>
      <description>&lt;P&gt;I'll add in the actual 'string' myself.&amp;nbsp; I don't at present want SAS to do that for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The brunt of the question is, can I change the top code shown to add in the 'string' variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see now that I can change each of the individual dataset files in a separate procedure as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sas_1.i_21503a_short_gt;
set sas_1.i_21503a_short_gt;
Path='2_gt';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This adds in the variable "Path", with each component "&lt;CODE class=" language-sas"&gt;2_gt".&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;So,&amp;nbsp;again,&amp;nbsp;is&amp;nbsp;it&amp;nbsp;possible&amp;nbsp;to&amp;nbsp;adjust&amp;nbsp;the&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;very&amp;nbsp;top&amp;nbsp;code&amp;nbsp;to&amp;nbsp;do&amp;nbsp;the&amp;nbsp;same?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 07:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758725#M239627</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2021-08-02T07:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: Add in new variable to dataset, with same 'string' for every line</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758727#M239628</link>
      <description>&lt;P&gt;Indeed....&lt;/P&gt;
&lt;P&gt;I see now, simply add the following just above "run;":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Path='2_gt';
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks to both of you for jogging me along.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Aug 2021 07:59:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-in-new-variable-to-dataset-with-same-string-for-every-line/m-p/758727#M239628</guid>
      <dc:creator>NKormanik</dc:creator>
      <dc:date>2021-08-02T07:59:14Z</dc:date>
    </item>
  </channel>
</rss>

