<?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: How to combine two variables into one variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717960#M222109</link>
    <description>Thank you for the explanation. I tried to use the Coalesce function, and it works well! Thanks.&lt;BR /&gt;</description>
    <pubDate>Tue, 09 Feb 2021 17:52:10 GMT</pubDate>
    <dc:creator>walker105</dc:creator>
    <dc:date>2021-02-09T17:52:10Z</dc:date>
    <item>
      <title>How to combine two variables into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717780#M222025</link>
      <description>&lt;P&gt;Hi, Community&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a new variable combining two variables. My data looks like this (if pre_a has a value, post_a doesn't. If post_a has a value, pre_a doesn't..):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;pre_a post_a pre_b post_b&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create a new variable 'Apple' combining pre_a and post_a, and 'Ball' combining pre_b and post_b. The dataset I would like to get looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;pre_a post_a apple&amp;nbsp; &amp;nbsp; &amp;nbsp;pre_b post_b ball&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp;&lt;/P&gt;&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;apple=pre_a; if apple ne ' ' then output;&lt;BR /&gt;apple=post_a; if apple ne ' ' then output;&lt;/P&gt;&lt;P&gt;ball=pre_b; if ball&amp;nbsp; ne ' ' then output;&lt;BR /&gt;ball=post_b; if ball&amp;nbsp; ne ' ' then output;&lt;/P&gt;&lt;P&gt;keep apple ball; run;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If using the code, I got the apple variable, correctly, but not a ball variable. Could you give some advice to do this? Or, is there different way to create a new variable combining two variables?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your answers would be very helpful! Thanks!&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>Tue, 09 Feb 2021 05:26:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717780#M222025</guid>
      <dc:creator>walker105</dc:creator>
      <dc:date>2021-02-09T05:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine two variables into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717785#M222030</link>
      <description>&lt;P&gt;Take a look at the&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p1vjttz6nuankzn1gh4z3wgcu0bf.htm&amp;amp;locale=en" target="_self"&gt;Coalesce&lt;/A&gt;&amp;nbsp; function.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 05:37:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717785#M222030</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-09T05:37:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine two variables into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717790#M222034</link>
      <description>&lt;P&gt;And what should the value of "apple" be, if pre_a = 4 and post_a = 5? Or is it impossible that both variable are non-missing?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 06:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717790#M222034</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-02-09T06:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine two variables into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717793#M222035</link>
      <description>&lt;P&gt;Part of what I believe to be your problem is that when you use OUTPUT the second variable has not been set yet. If you check the log you should see that your output data set has close to twice as many output records and input unless you have many records where both the pre and post values are missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't tell whether your variables are character or not (too many people mix statements with inappropriate operations for variable type)&lt;/P&gt;
&lt;P&gt;Here is an example similar to the logic you are using. I am using numeric values but the behavior is similar with character.&lt;/P&gt;
&lt;PRE&gt;data have;
   input x1 x2 y1 y2;
datalines;
1 . . 2
. 2 . 2
1 . 2 .
;

data example;
   set have;
   xx = x1;
   if xx ne . then output;
   xx = x2;
   if xx ne . then output;
   yy = y1;
   if yy ne . then output;
   yy = y2;
   if yy ne . then output;
run;
&lt;/PRE&gt;
&lt;P&gt;Notice that the Example data set has 6 records in the output when only 3 are read in. Also the YY value is set on the 2nd, 4th and 6th records.&lt;/P&gt;
&lt;P&gt;That is why I think you want the coalescec or coalesce function as suggested by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;. Example with the same data:&lt;/P&gt;
&lt;PRE&gt;data better;
   set have;
   xx= coalesce (x1,x2);
   yy= coalesce (y1,y2);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your variables are character then this may go what you are expecting:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   apple = coalescec (pre_a, post_a);
   ball = coalescec (pre_b, post_b);
run;
&lt;/PRE&gt;
&lt;P&gt;if the values are actually numeric instead of character the function would be Coalesce instead.&lt;/P&gt;
&lt;P&gt;The two functions Coalesce and Coalescec return the first value found in the list of values which could be considerably more than just 2 values or variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you have questions about unexpected behavior of code the best thing is to copy the entire data step or procedure code from the log with all the messages or warnings and then paste into a text box opened on the forum using the &amp;lt;/&amp;gt; such as:&lt;/P&gt;
&lt;PRE&gt;134  data example;
135     set have;
136     xx = x1;
137     if xx ne . then output;
138     xx = x2;
139     if xx ne . then output;
140     yy = y1;
141     if yy ne . then output;
142     yy = y2;
143     if yy ne . then output;
144  run;

NOTE: There were 3 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.EXAMPLE has 6 observations and 6 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

&lt;/PRE&gt;
&lt;P&gt;The notes here clearly show reading 3 records and output 6. Since your example data has missing values for some of the pairs the number would not be exactly twice the number on the input set but I would expect it to be notably larger.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also make it harder to tell what happened by only keeping the resulting Apple and Ball variables. When debugging code behavior you want to keep all of the variables that are involved so you can see what is calculated from which values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 18:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717793#M222035</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-09T18:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine two variables into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717959#M222108</link>
      <description>Thanks! The Coalesce function works well for my data!&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Feb 2021 17:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717959#M222108</guid>
      <dc:creator>walker105</dc:creator>
      <dc:date>2021-02-09T17:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine two variables into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717960#M222109</link>
      <description>Thank you for the explanation. I tried to use the Coalesce function, and it works well! Thanks.&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Feb 2021 17:52:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-combine-two-variables-into-one-variable/m-p/717960#M222109</guid>
      <dc:creator>walker105</dc:creator>
      <dc:date>2021-02-09T17:52:10Z</dc:date>
    </item>
  </channel>
</rss>

