<?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: Concatenate strings with missing values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401830#M97511</link>
    <description>&lt;P&gt;Assumes sorted:&lt;/P&gt;
&lt;P&gt;Edit, changed as I don' think original will work: - As noted this is where test data so I can run things comes in useful!&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by rid;&lt;BR /&gt;  length qs $50;
  if first.rid then qs="";&lt;BR /&gt;  substr(qs,input(tranwrd(questions,"qt",""),best.),1)=strip(resp);&lt;BR /&gt;  if last.rid then output;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Old:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by rid;
  resp=ifc(first.rid,response,cat(resp,ifc(resp=""," ",resp)));
  if last.rid then output;
run;
&lt;/PRE&gt;
&lt;P&gt;It a good idea to post test data in the form of a datastep in the text of the post, as such the above is not tested.&lt;/P&gt;</description>
    <pubDate>Fri, 06 Oct 2017 16:19:16 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-10-06T16:19:16Z</dc:date>
    <item>
      <title>Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401827#M97510</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I have some data :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;rid&amp;nbsp; &amp;nbsp;form&amp;nbsp; &amp;nbsp;questions&amp;nbsp; response&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; qt1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; qt2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; qt3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&lt;/P&gt;&lt;P&gt;......&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;qt50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; D&lt;/P&gt;&lt;P&gt;......&lt;/P&gt;&lt;P&gt;24&amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; qt1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;24&amp;nbsp; &amp;nbsp; &amp;nbsp;F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;qt2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;24&amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; qt3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want my output like&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; ABC.......D&lt;/P&gt;&lt;P&gt;.....&lt;/P&gt;&lt;P&gt;24&amp;nbsp; &amp;nbsp;B C.........&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if candidates did not answer, the output need the space for that question.&lt;/P&gt;&lt;P&gt;Any reply is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 16:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401827#M97510</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-10-06T16:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401830#M97511</link>
      <description>&lt;P&gt;Assumes sorted:&lt;/P&gt;
&lt;P&gt;Edit, changed as I don' think original will work: - As noted this is where test data so I can run things comes in useful!&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by rid;&lt;BR /&gt;  length qs $50;
  if first.rid then qs="";&lt;BR /&gt;  substr(qs,input(tranwrd(questions,"qt",""),best.),1)=strip(resp);&lt;BR /&gt;  if last.rid then output;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Old:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by rid;
  resp=ifc(first.rid,response,cat(resp,ifc(resp=""," ",resp)));
  if last.rid then output;
run;
&lt;/PRE&gt;
&lt;P&gt;It a good idea to post test data in the form of a datastep in the text of the post, as such the above is not tested.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 16:19:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401830#M97511</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-06T16:19:16Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401835#M97512</link>
      <description>&lt;P&gt;data test;&lt;BR /&gt;input id$10. form$3. question$10. response$3.;&lt;BR /&gt;datalines;&lt;BR /&gt;123456789 F question1 A&lt;BR /&gt;123456789 F question2 B&lt;BR /&gt;123456789 F question3 C&lt;BR /&gt;123456789 F question4 D&lt;BR /&gt;234567891 F question1 A&lt;BR /&gt;234567891 F question2 A&lt;BR /&gt;234567891 F question3 D&lt;BR /&gt;234567891 F question4 D&lt;BR /&gt;345678912 F question1 A&lt;BR /&gt;345678912 F question2&lt;BR /&gt;345678912 F question3 C&lt;BR /&gt;345678912 F question4 D&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 16:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401835#M97512</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-10-06T16:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401838#M97514</link>
      <description>&lt;P&gt;Thanks, this works:&lt;/P&gt;
&lt;PRE&gt;data test;
  infile datalines missover;
  length id form question response $10;
  input id $ form $ question $ response $;
datalines;
123456789 F question1 A
123456789 F question2 B
123456789 F question3 C
123456789 F question4 D
234567891 F question1 A
234567891 F question2 A
234567891 F question3 D
234567891 F question4 D
345678912 F question1 A
345678912 F question2
345678912 F question3 C
345678912 F question4 D
;
run;
data want;
  set test;
  by id;
  length qs $50;
  retain qs;
  if first.id then qs="";
  substr(qs,input(tranwrd(question,"question",""),best.),1)=strip(response);
  if last.id then output;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Oct 2017 16:30:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401838#M97514</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-06T16:30:58Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401842#M97515</link>
      <description>&lt;P&gt;Thanks for help, the code works for my test code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 16:42:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401842#M97515</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-10-06T16:42:36Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401843#M97516</link>
      <description>&lt;P&gt;However the third id didn't answer question2 and the output of qs need the blank at the second location.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 16:44:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401843#M97516</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-10-06T16:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401853#M97517</link>
      <description>&lt;P&gt;You could use SUBSTR() function as the TARGET of an assignment statement to place the response in the proper place.&lt;/P&gt;
&lt;P&gt;This code works for you example. It assumes that all groups have all questions and they are in the right order.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  length id $10 form $3 question $10 response $3;
  input id--response ;
datalines;
123456789 F question1 A
123456789 F question2 B
123456789 F question3 C
123456789 F question4 D
234567891 F question1 A
234567891 F question2 A
234567891 F question3 D
234567891 F question4 D
345678912 F question1 A
345678912 F question2 .
345678912 F question3 C
345678912 F question4 D
;

data want ;
  do q=1 by 1 until (last.form);
    set test ;
    by id form ;
    length want $10 ;
    substr(want,q,1)=response  ;
  end;
  drop q question response;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs       id        form    want

 1     123456789     F      ABCD
 2     234567891     F      AADD
 3     345678912     F      A CD&lt;/PRE&gt;
&lt;P&gt;If your data is not sorted by QUESTION or some rows are missing then instead of generating Q from the record order you could derive it from the value of the QUESTION variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  do until (last.form);
    set test ;
    by id form ;
    length want $10 ;
    q=input(compress(question,' ','dk'),32.);
    substr(want,q,1)=response  ;
  end;
  drop q question response;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Oct 2017 17:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401853#M97517</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-06T17:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401895#M97535</link>
      <description>&lt;P&gt;Sorry, what is wrong with my code then.&amp;nbsp; When I run it the one record with a missing character is a space in the output - exactly what you asked for??&amp;nbsp; Basically it checks each question number against the character position in the output string, so missing will be a missing character in the string.&amp;nbsp; Please provide examples - test / output - if something has changed.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 19:24:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401895#M97535</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-10-06T19:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate strings with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401917#M97542</link>
      <description>&lt;P&gt;Thank you very much! Tom, I solve that problem with discussion with my colleague. I appreciate your time and help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 20:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-strings-with-missing-values/m-p/401917#M97542</guid>
      <dc:creator>daisy6</dc:creator>
      <dc:date>2017-10-06T20:16:59Z</dc:date>
    </item>
  </channel>
</rss>

