<?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: Powershell command using Call System - replace text in xml file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/359649#M84586</link>
    <description>&lt;P&gt;Reeza,&lt;/P&gt;
&lt;P&gt;You could save these os command as a BAT file (e.g. make a text file which has BAT extension name&lt;/P&gt;
&lt;P&gt;and contain all the command you need, it is easy to do by data step ),and execute it at SAS side.&lt;/P&gt;
&lt;P&gt;E.X.&lt;/P&gt;
&lt;P&gt;c:\temp\xx.bat&amp;nbsp;&lt;/P&gt;
&lt;P&gt;has the os command you need like :&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token string"&gt;c:\temp\powershell.exe -command &lt;/SPAN&gt;&lt;SPAN class="token string"&gt;(gc "&amp;amp;path_file") | ForEach-Object &lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;can execute it &amp;nbsp;once for all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x 'c:\temp\x.bat' ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 18 May 2017 13:49:33 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2017-05-18T13:49:33Z</dc:date>
    <item>
      <title>Powershell command using Call System - replace text in xml file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/359514#M84554</link>
      <description>&lt;P&gt;I have an XML file and I need to update a single variable in the XML files (~100 files). &amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've managed to get the command working in powershell, but can't seem to figure out how to pass this to SAS to execute.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm having trouble with getting the correct number of quotation marks for my macro variables to resolve and passing this to the system somehow.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code so far is below and I'm attaching the xml file for testing as well, with a TXT extension. It has a single word in it, Spatial_Area that would need to be updated - this is only for testing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I feel like the solution is obvious but it's the end of the day after meetings and my brain is fried so any assistance is appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a SAS solution to replace the field using SAS, I wouldn't be averse to that either, I just need to finish this at this point before the long weekend &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path_file = C:\_localdata\Spatial_Area.xml;

data CHECK;
application_exe = "powershell.exe -command ";
command = '(gc "&amp;amp;path_file") | ForEach-Object { $_ -replace "Spatial_Area", "CD 1" } | sc "&amp;amp;path_file"';
cmd = application_exe || '"' || command || '"'; 
 *Place note in log;
  putlog "NOTE-Processing command" cmd;
 *Execute command;
  call system(cmd);

RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 May 2017 23:38:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/359514#M84554</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-05-17T23:38:53Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell command using Call System - replace text in xml file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/359526#M84556</link>
      <description>&lt;P&gt;The single quote is masking resolution of the &amp;amp;path_file macrovar.&amp;nbsp; Even though it occurs in double quotes, it's inside the singel quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So use&amp;nbsp;double quotes throughout, noting that internal double quotes must be "escaped" by doubling each of them.&amp;nbsp; I.e. instead of&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Sasfont"&gt; command = &lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'(gc "&amp;amp;path_file") | ForEach-Object { $_ -replace "Spatial_Area", "CD 1" } | sc "&amp;amp;path_file"'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Sasfont"&gt;use&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Sasfont"&gt; command = &lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;"(gc ""&amp;amp;path_file"") | ForEach-Object { $_ -replace ""Spatial_Area"", ""CD 1"" } | sc ""&amp;amp;path_file"""&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 01:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/359526#M84556</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-05-18T01:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell command using Call System - replace text in xml file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/359649#M84586</link>
      <description>&lt;P&gt;Reeza,&lt;/P&gt;
&lt;P&gt;You could save these os command as a BAT file (e.g. make a text file which has BAT extension name&lt;/P&gt;
&lt;P&gt;and contain all the command you need, it is easy to do by data step ),and execute it at SAS side.&lt;/P&gt;
&lt;P&gt;E.X.&lt;/P&gt;
&lt;P&gt;c:\temp\xx.bat&amp;nbsp;&lt;/P&gt;
&lt;P&gt;has the os command you need like :&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token string"&gt;c:\temp\powershell.exe -command &lt;/SPAN&gt;&lt;SPAN class="token string"&gt;(gc "&amp;amp;path_file") | ForEach-Object &lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;can execute it &amp;nbsp;once for all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x 'c:\temp\x.bat' ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 13:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/359649#M84586</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-05-18T13:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell command using Call System - replace text in xml file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/360952#M85058</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ended up having to add single quotes around the path as well because the file names had spaces. Without spaces it works fine, but with the spaces it had issues.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally, rather&amp;nbsp;than the ForEach I did find that PowerShell has a REPLACE() method (term?) that worked and the syntax for that was easier to manage.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again, thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&amp;amp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;My colleague did go down the bat file route and that did work as well, so thanks for that suggestion. She learned a lot from the process.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 May 2017 22:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Powershell-command-using-Call-System-replace-text-in-xml-file/m-p/360952#M85058</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-05-23T22:51:10Z</dc:date>
    </item>
  </channel>
</rss>

