<?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 Conditionally adding a string to a character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-adding-a-string-to-a-character-variable/m-p/116603#M24054</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a data file with a number of numeric variables with values of 0 or 1, as well as a few case summary character variables.&amp;nbsp; As a data checking procedure, we want to look at observations that have multiple variables with a value of 1.&amp;nbsp; While I could simply run a proc print of all of the numeric variables, I need to be able to distribute paper copies of the information and there are too many variables for a legible print.&amp;nbsp; My thought had been to create a new character variable in the data step and concatenate the name of each variable with a value of 1 onto it, but I cannot get that to work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's what I have:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data checkfile; set xxx.xxx;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length multi $140;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if var1 = 1 then multi = "var1name";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if var2 = 1 then multi = multi || " var2name";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if var&lt;EM&gt;n&lt;/EM&gt; = 1 then multi = multi || " var&lt;EM&gt;n&lt;/EM&gt;name";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I run a proc freq, all I get in return is the results from the first if statement.&amp;nbsp; SAS isn't throwing up any red flags, so I'm at a bit of a loss as to what's going wrong.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 24 Jul 2012 15:01:35 GMT</pubDate>
    <dc:creator>scify</dc:creator>
    <dc:date>2012-07-24T15:01:35Z</dc:date>
    <item>
      <title>Conditionally adding a string to a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-adding-a-string-to-a-character-variable/m-p/116603#M24054</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a data file with a number of numeric variables with values of 0 or 1, as well as a few case summary character variables.&amp;nbsp; As a data checking procedure, we want to look at observations that have multiple variables with a value of 1.&amp;nbsp; While I could simply run a proc print of all of the numeric variables, I need to be able to distribute paper copies of the information and there are too many variables for a legible print.&amp;nbsp; My thought had been to create a new character variable in the data step and concatenate the name of each variable with a value of 1 onto it, but I cannot get that to work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's what I have:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data checkfile; set xxx.xxx;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; length multi $140;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if var1 = 1 then multi = "var1name";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if var2 = 1 then multi = multi || " var2name";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if var&lt;EM&gt;n&lt;/EM&gt; = 1 then multi = multi || " var&lt;EM&gt;n&lt;/EM&gt;name";&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I run a proc freq, all I get in return is the results from the first if statement.&amp;nbsp; SAS isn't throwing up any red flags, so I'm at a bit of a loss as to what's going wrong.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Jul 2012 15:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-adding-a-string-to-a-character-variable/m-p/116603#M24054</guid>
      <dc:creator>scify</dc:creator>
      <dc:date>2012-07-24T15:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally adding a string to a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-adding-a-string-to-a-character-variable/m-p/116604#M24055</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try using&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if var2 = 1 then multi = catx(' ',multi,"var2name");&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;what is happening is multi was padded with blanks to make 140 characters and the || operator was attempting to append past that length. The CATX function will strip variables of leading and trailing blanks to make things fit better.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Jul 2012 15:12:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-adding-a-string-to-a-character-variable/m-p/116604#M24055</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2012-07-24T15:12:49Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally adding a string to a character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-adding-a-string-to-a-character-variable/m-p/116605#M24056</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There's actually a much easier way to achieve your goal, without having to write multiple IF statements.&amp;nbsp; Just store the var1-varn variables in an array, loop through each one to check for the value 1, then use the VNAME function to add that variable name to the concatenated list.&amp;nbsp; Your code will look something like this, I've put in 2 options for the 'multi' variable, the first stores the results as a comma separated list, the 2nd puts double quotes around each value as per your code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data checkfile;&lt;/P&gt;&lt;P&gt;set xxx.xxx; &lt;/P&gt;&lt;P&gt;length multi $140; &lt;/P&gt;&lt;P&gt;array vars{*} var1--varn; &lt;/P&gt;&lt;P&gt;do i=1 to dim(vars); &lt;/P&gt;&lt;P&gt;if vars{i}=1 then call catx(',',multi,vname(vars{i})); &lt;/P&gt;&lt;P&gt;/* or */&lt;/P&gt;&lt;P&gt;if vars{i}=1 then call catx('',multi,quote(vname(vars{i}))); &lt;/P&gt;&lt;P&gt;end; &lt;/P&gt;&lt;P&gt;drop i; &lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 24 Jul 2012 15:55:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-adding-a-string-to-a-character-variable/m-p/116605#M24056</guid>
      <dc:creator>Keith</dc:creator>
      <dc:date>2012-07-24T15:55:08Z</dc:date>
    </item>
  </channel>
</rss>

