<?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: Dynamically writing SAS variable values as key value pairs to JSON file using PUT statements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330345#M74083</link>
    <description>&lt;P&gt;If all your variables are the same type using an array may be easier than a macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add an if condition to only put values if not missing. The MISSING() function will work on character or numeric variables.&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;if not missing(var) then do;
put...;
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To remove space after variable name, back up the point using another PUT or adding it to your statement.&lt;/P&gt;
&lt;P&gt;See the solution here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/How-to-remove-trailing-blanks-when-PUT-statement-resolves-for/m-p/190655/highlight/true#M7261" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/How-to-remove-trailing-blanks-when-PUT-statement-resolves-for/m-p/190655/highlight/true#M7261&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put variable +(-1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Feb 2017 22:28:53 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-02-06T22:28:53Z</dc:date>
    <item>
      <title>Dynamically writing SAS variable values as key value pairs to JSON file using PUT statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330335#M74075</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am writing to JSON file using SAS dataset as input using below code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT face="Courier New"&gt;file _webout PS=&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New"&gt;32767&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New"&gt; ;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;set &amp;amp;indata end=lastrec;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT face="Courier New"&gt;put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;' "header":{'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; /; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New"&gt;/*header starts*/&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;%do&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; i = &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New"&gt;%to&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New"&gt;14&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT face="Courier New"&gt;put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;' "'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; @; put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;"%trim(&amp;amp;&amp;amp;var&amp;amp;i)"&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;@; put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;'":"'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;@; put &amp;amp;&amp;amp;var&amp;amp;i &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;'",'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT color="#0000ff" face="Courier New"&gt;%end&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT face="Courier New"&gt;put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;' "'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; @; put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;"%trim(&amp;amp;&amp;amp;var15)"&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;@; put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;'":"'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; &amp;amp;&amp;amp;var15&lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;'"'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;&lt;FONT face="Courier New"&gt;put &lt;/FONT&gt;&lt;FONT color="#800080" face="Courier New"&gt;' },'&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; /; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New"&gt;/*header ends*/&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the output message, it puts and extra space for variable value when writing to file. Please see output file below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"header":{&lt;/P&gt;&lt;P&gt;　&lt;/P&gt;&lt;P&gt;"senderMessageId":"senderMessageId ",&lt;/P&gt;&lt;P&gt;"senderMessageName":"getDecision ",&lt;/P&gt;&lt;P&gt;"versionNumber":"1 ",&lt;/P&gt;&lt;P&gt;"sequenceNumber":"1 ",&lt;/P&gt;&lt;P&gt;"senderMessageIdEcho":" ",&lt;/P&gt;&lt;P&gt;"sessionId":"16f526829a0f48f29a6a144d2f1bcd9d ",&lt;/P&gt;&lt;P&gt;"creationTimeStamp":"01JAN60:00:00:00 ",&lt;/P&gt;&lt;P&gt;"senderApplicationId":"ZT ",&lt;/P&gt;&lt;P&gt;"senderSubApplicationId":" ",&lt;/P&gt;&lt;P&gt;"senderHostName":"eai-CDEV.com ",&lt;/P&gt;&lt;P&gt;"billingAU":". ",&lt;/P&gt;&lt;P&gt;"customerId":"12345 ",&lt;/P&gt;&lt;P&gt;"teamMemberId":" ",&lt;/P&gt;&lt;P&gt;"environment":"CDEV ",&lt;/P&gt;&lt;P&gt;"applicationId":" "&lt;/P&gt;&lt;P&gt;},&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not need this extra space at end of value for each variable value. Also, it puts two spaces for blank character variables, I do not need that. I want nothing to be written to file when character or numeric variable is missing. Please help on this if someone has faced similar issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Feb 2017 22:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330335#M74075</guid>
      <dc:creator>jazz17</dc:creator>
      <dc:date>2017-02-06T22:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically writing SAS variable values as key value pairs to JSON file using PUT statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330345#M74083</link>
      <description>&lt;P&gt;If all your variables are the same type using an array may be easier than a macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Add an if condition to only put values if not missing. The MISSING() function will work on character or numeric variables.&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;if not missing(var) then do;
put...;
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To remove space after variable name, back up the point using another PUT or adding it to your statement.&lt;/P&gt;
&lt;P&gt;See the solution here:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/How-to-remove-trailing-blanks-when-PUT-statement-resolves-for/m-p/190655/highlight/true#M7261" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/How-to-remove-trailing-blanks-when-PUT-statement-resolves-for/m-p/190655/highlight/true#M7261&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;put variable +(-1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Feb 2017 22:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330345#M74083</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-02-06T22:28:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically writing SAS variable values as key value pairs to JSON file using PUT statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330349#M74086</link>
      <description>&lt;P&gt;When you&amp;nbsp;&lt;EM&gt;put&lt;/EM&gt; a variable, rather than a character string, and it doesn't have an specific format, it&amp;nbsp;&lt;EM&gt;always&amp;nbsp;&lt;/EM&gt;suffixes it with a space. The way I always do it is to use the pointer code&amp;nbsp;+(-1)&lt;EM&gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Put that after the value&amp;nbsp;&lt;EM&gt;&amp;amp;&amp;amp;var&amp;amp;i&lt;/EM&gt;, and things should be different, if not spot on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you can check the value of &amp;amp;&amp;amp;var&amp;amp;i with the&amp;nbsp;&lt;EM&gt;missing&lt;/EM&gt; function. Like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if not missing(&amp;amp;&amp;amp;var&amp;amp;i) then do; 
   put…&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Feb 2017 22:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330349#M74086</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-02-06T22:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically writing SAS variable values as key value pairs to JSON file using PUT statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330983#M74337</link>
      <description>&lt;P&gt;Thank you Reeza! This worked! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 21:03:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330983#M74337</guid>
      <dc:creator>jazz17</dc:creator>
      <dc:date>2017-02-08T21:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically writing SAS variable values as key value pairs to JSON file using PUT statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330984#M74338</link>
      <description>&lt;P&gt;Thank you Laurie! This worked &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 21:03:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/330984#M74338</guid>
      <dc:creator>jazz17</dc:creator>
      <dc:date>2017-02-08T21:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically writing SAS variable values as key value pairs to JSON file using PUT statements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/331358#M74453</link>
      <description>&lt;P&gt;FYI - If you are running SAS 9.4, the &lt;A href="http://support.sas.com/documentation/cdl/en/proc/69850/HTML/default/viewer.htm#p0ie4bw6967jg6n1iu629d40f0by.htm" target="_self"&gt;JSON procedure&lt;/A&gt; can write SAS data to a JSON file.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2017 22:27:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-writing-SAS-variable-values-as-key-value-pairs-to/m-p/331358#M74453</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2017-02-09T22:27:28Z</dc:date>
    </item>
  </channel>
</rss>

