<?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: Concatenating string variables based on IF-THEN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371355#M88711</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/43822"&gt;@Wolverine&lt;/a&gt;: In your initial datastep you referenced a library that you never declared AND didn't include in your proc import.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ran your code after removing the libname and it appeared to run as expected. Here is the modified datastep I used:&lt;/P&gt;
&lt;PRE&gt;DATA /*DISNtemp.*/DISN_open_end_recode1TEST;
  KEEP resRespondent
       O_Q1AR1M1
       O_Q1AR1M1_Notes
       O_Q1AR1M2
       O_Q1AR1M2_Notes
       O_Q1AR1M3
       O_Q1AR1M3_Notes;
  SET /*DISNtemp.*/DISN_open_end_imported;
RUN;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, while you set the three notes' variable to missing if you find a 68, you never set them to the values you want to keep. e.g., you use:&lt;/P&gt;
&lt;PRE&gt;  IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M1 = "" ;
&lt;/PRE&gt;
&lt;P&gt;but you never set O_Q1AR1_M1 to the value that you want. I think you meant to use something like:&lt;/P&gt;
&lt;PRE&gt;  IF not(O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) THEN do;
    O_Q1AR1_M1 = O_Q1AR1M1;
    O_Q1AR1_M2 = O_Q1AR1M2;
    O_Q1AR1_M3 = O_Q1AR1M3;
  end;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Wed, 28 Jun 2017 16:16:01 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-06-28T16:16:01Z</dc:date>
    <item>
      <title>Concatenating string variables based on IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371293#M88687</link>
      <description>&lt;P&gt;I have a somewhat messy Excel file (which I've imported into SAS) that was used to recode open-ended survey responses into a set of categories.&amp;nbsp; However, there is a "68-Other" code, and when that code is present, I need to keep the open-ended response.&amp;nbsp; Otherwise, the open-ended response should be deleted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Making things a bit more complicated is that both the recodes and the open-ended responses are spread across several variables.&amp;nbsp; The end file should have 3 recode variables and 1 open-end variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, in English, the code is supposed to do this: Look at all 3 of the recode variables.&amp;nbsp; If none of them are 68, then make the open-ended variables blank.&amp;nbsp; If any of the recode variables are 68, the open-ended variables should not be changed.&amp;nbsp; Then take the 3 open-ended variables and concatenate them into a single open-ended variable (named O_Q1AR1_recode_all_text).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In reality, the code doesn't make the open-ended variables blank even when 68 is not present.&amp;nbsp; And O_Q1AR1_recode_all_text always has the value ";;" whether the open-ended variables are blank or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*THIS ISN'T WORKING FOR SOME REASON*/
/*If none of the codes = 68-Other, then the text variables should be blank*/
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M1 = "" ;
	
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M2 = "" ;
	
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M3 = "" ;	


/*THIS ISN'T WORKING FOR SOME REASON*/
/*Concatenate the text variables*/
O_Q1AR1_recode_all_text = O_Q1AR1_M1||";"||O_Q1AR1_M2||";"||O_Q1AR1_M3;

RUN;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 13:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371293#M88687</guid>
      <dc:creator>Wolverine</dc:creator>
      <dc:date>2017-06-28T13:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating string variables based on IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371304#M88694</link>
      <description>&lt;P&gt;You didn't provide a subset of your data as an example, so I had to roll my own. Using that data, your code seemed to work fine:&lt;/P&gt;
&lt;PRE&gt;data have;
  input O_Q1AR1_recode1-O_Q1AR1_recode3 (O_Q1AR1_M1-O_Q1AR1_M3) ($);
  cards;
2 68 3 x y z
4 3 2 a b c
55 21 68 d e f
;

/*THIS ISN'T WORKING FOR SOME REASON*/
/*If none of the codes = 68-Other, then the text variables should be blank*/
data want;
set have;
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M1 = "" ;
	
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M2 = "" ;
	
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M3 = "" ;	


/*THIS ISN'T WORKING FOR SOME REASON*/
/*Concatenate the text variables*/
O_Q1AR1_recode_all_text = O_Q1AR1_M1||";"||O_Q1AR1_M2||";"||O_Q1AR1_M3;

RUN;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 13:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371304#M88694</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-28T13:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating string variables based on IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371305#M88695</link>
      <description>&lt;P&gt;Please post example data. Use the macro provided in&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; to convert the imported SAS dataset into a datastep for posting here.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 13:58:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371305#M88695</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-28T13:58:28Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating string variables based on IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371307#M88697</link>
      <description>&lt;P&gt;Variables have a fixed length in SAS.&amp;nbsp; At a minimum, consider adding the TRIM function to eliminate excessive blanks:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;O_Q1AR1_recode_all_text = trim(O_Q1AR1_M1)||";"||trim(O_Q1AR1_M2)||";"||O_Q1AR1_M3;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 14:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371307#M88697</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-28T14:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating string variables based on IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371334#M88704</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IMPORT datafile= "YOUR PATH\DISN_Completes_Open_End_TEST.xlsx"&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;out=DISN_open_end_imported&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;dbms=xlsx REPLACE;&lt;BR /&gt;sheet="DISN Completes Notes";&lt;BR /&gt;getnames=yes;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;DATA DISNtemp.DISN_open_end_recode1TEST; &lt;BR /&gt; KEEP resRespondent O_Q1AR1M1 O_Q1AR1M1_Notes O_Q1AR1M2 O_Q1AR1M2_Notes O_Q1AR1M3&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;O_Q1AR1M3_Notes;&lt;BR /&gt;SET DISNtemp.DISN_open_end_imported;&lt;BR /&gt;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;DATA DISN_open_end_recode2TEST; SET DISN_open_end_recode1TEST;


/*Concatenate the "Notes" variables*/
O_Q1AR1_recode_all = O_Q1AR1M1_Notes||O_Q1AR1M2_Notes||O_Q1AR1M3_Notes;

/*Remove blanks, commas, parentheses, and perods*/
O_Q1AR1_recode_compr = compress(O_Q1AR1_recode_all, ' (),.');

/*Separate the concatenated codes into separate variables*/
O_Q1AR1_recode1 = SUBSTR(O_Q1AR1_recode_compr,1,2);
O_Q1AR1_recode2 = SUBSTR(O_Q1AR1_recode_compr,3,2);
O_Q1AR1_recode3 = SUBSTR(O_Q1AR1_recode_compr,5,2);


/*THIS ISN'T WORKING FOR SOME REASON*/
/*If none of the codes = 68-Other, then the text variables should be blank*/
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M1 = "" ;
	
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M2 = "" ;
	
IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M3 = "" ;	


/*THIS ISN'T WORKING FOR SOME REASON*/
/*Concatenate the text variables*/
O_Q1AR1_recode_all_text = TRIM(O_Q1AR1_M1)||";"||TRIM(O_Q1AR1_M2)||";"||TRIM(O_Q1AR1_M3);

RUN;&lt;BR /&gt;&lt;BR /&gt;title1 "Q1A concatenation";&lt;BR /&gt;PROC FREQ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;TABLES O_Q1AR1_recode_all O_Q1AR1_recode_compr O_Q1AR1_recode1 O_Q1AR1_recode2 O_Q1AR1_recode3 O_Q1AR1_recode_all_text&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;O_Q1AR1_M1;&lt;BR /&gt;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I attached a small portion of the data file, along with a larger section of my code (the first part of the code appears to be working fine).&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 15:00:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371334#M88704</guid>
      <dc:creator>Wolverine</dc:creator>
      <dc:date>2017-06-28T15:00:02Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating string variables based on IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371355#M88711</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/43822"&gt;@Wolverine&lt;/a&gt;: In your initial datastep you referenced a library that you never declared AND didn't include in your proc import.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I ran your code after removing the libname and it appeared to run as expected. Here is the modified datastep I used:&lt;/P&gt;
&lt;PRE&gt;DATA /*DISNtemp.*/DISN_open_end_recode1TEST;
  KEEP resRespondent
       O_Q1AR1M1
       O_Q1AR1M1_Notes
       O_Q1AR1M2
       O_Q1AR1M2_Notes
       O_Q1AR1M3
       O_Q1AR1M3_Notes;
  SET /*DISNtemp.*/DISN_open_end_imported;
RUN;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, while you set the three notes' variable to missing if you find a 68, you never set them to the values you want to keep. e.g., you use:&lt;/P&gt;
&lt;PRE&gt;  IF (O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) 
	THEN O_Q1AR1_M1 = "" ;
&lt;/PRE&gt;
&lt;P&gt;but you never set O_Q1AR1_M1 to the value that you want. I think you meant to use something like:&lt;/P&gt;
&lt;PRE&gt;  IF not(O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) THEN do;
    O_Q1AR1_M1 = O_Q1AR1M1;
    O_Q1AR1_M2 = O_Q1AR1M2;
    O_Q1AR1_M3 = O_Q1AR1M3;
  end;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 16:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371355#M88711</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-28T16:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenating string variables based on IF-THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371400#M88722</link>
      <description>&lt;P&gt;Sorry, I forgot to remove the libname from the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, this revised code fixed the issue:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;IF not(O_Q1AR1_recode1 ^=68 AND O_Q1AR1_recode2 ^=68 AND O_Q1AR1_recode3 ^=68) THEN do;
    O_Q1AR1_M1 = O_Q1AR1M1;
    O_Q1AR1_M2 = O_Q1AR1M2;
    O_Q1AR1_M3 = O_Q1AR1M3;
  end;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I assumed that if the IF-THEN statement didn't assign a new value, then the variables would retain their original value.&amp;nbsp; But I see from your revised code that isn't the case!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://communities.sas.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 17:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenating-string-variables-based-on-IF-THEN/m-p/371400#M88722</guid>
      <dc:creator>Wolverine</dc:creator>
      <dc:date>2017-06-28T17:46:29Z</dc:date>
    </item>
  </channel>
</rss>

