<?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: Combine multiple rows into one variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-rows-into-one-variable/m-p/691151#M210319</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114470"&gt;@alexkim&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;ROW&amp;nbsp; VARIABLE&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AGE&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SEX&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HEIGHT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to combine the above multiple rows variable into one new variable&lt;/P&gt;
&lt;P&gt;ROW&amp;nbsp; &amp;nbsp;VARIABLENEW&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AGE=_AGE&amp;nbsp; &amp;nbsp; SEX=_SEX&amp;nbsp; &amp;nbsp; HEIGHT=_HEIGHT&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;Problem 1: the length maximum length of variablenew depends on the number of variables: for each variable max 32 chars + 1 for the equals sign. For alphanumeric variables can use the length of the variable. For numerics it is a bit more challenging if no format is assigned to the variable, the using 20 seems to be appropriate.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.class;
   set sashelp.class;
   format Weight commax10.2 Height 5.1;
run;

data _null_;
   set sashelp.vcolumn(where= (Libname = 'WORK' and Memname = 'CLASS')) end=jobDone;
   
   length varList $ 10000;
   retain maxLength 0 regEx varList;
   
   if _n_ = 1 then do;
      regEx = prxparse('/(\d+)\D?/');
   end;
   
   varList = catx(',', VarList, quote(trim(Name)));
   
   if Type = 'char' then do;
      maxLength = maxLength + Length;
   end;
   else do;
      if missing(Format) or not prxmatch(regEx, trim(Format)) then do;
         maxLength = maxLength + 20;
      end;
      else do;
         calcLength = input(prxposn(regEx, 1, Format), best.);
         put name= calcLength= Format=;
         maxLength = maxLength + calcLength;
      end;
   end;
      
   if jobDone then do;
      /* add space for equals sign and blanks */
      maxLength = maxLength + 2 * countw(varList);
      call symputx('maxLength', maxLength);
      call symputx('varList', varList);
   end;
run;

data want;
   set work.class;
   
   length row 8 variablenew $ &amp;amp;maxLength. _variable $ 32;
   
   row = _n_;
   
   do _variable = &amp;amp;varList.;
      variablenew = catx(' ', variablenew, catx('=', _variable, vvaluex(_variable)));
   end;
   
   keep row variablenew;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 13 Oct 2020 07:28:26 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2020-10-13T07:28:26Z</dc:date>
    <item>
      <title>Combine multiple rows into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-rows-into-one-variable/m-p/691141#M210315</link>
      <description>&lt;P&gt;ROW&amp;nbsp; VARIABLE&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AGE&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SEX&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HEIGHT&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to combine the above multiple rows variable into one new variable&lt;/P&gt;&lt;P&gt;ROW&amp;nbsp; &amp;nbsp;VARIABLENEW&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AGE=_AGE&amp;nbsp; &amp;nbsp; SEX=_SEX&amp;nbsp; &amp;nbsp; HEIGHT=_HEIGHT&lt;/P&gt;</description>
      <pubDate>Tue, 13 Oct 2020 05:45:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-rows-into-one-variable/m-p/691141#M210315</guid>
      <dc:creator>alexkim</dc:creator>
      <dc:date>2020-10-13T05:45:47Z</dc:date>
    </item>
    <item>
      <title>Re: Combine multiple rows into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-rows-into-one-variable/m-p/691151#M210319</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114470"&gt;@alexkim&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;ROW&amp;nbsp; VARIABLE&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AGE&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SEX&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HEIGHT&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to combine the above multiple rows variable into one new variable&lt;/P&gt;
&lt;P&gt;ROW&amp;nbsp; &amp;nbsp;VARIABLENEW&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AGE=_AGE&amp;nbsp; &amp;nbsp; SEX=_SEX&amp;nbsp; &amp;nbsp; HEIGHT=_HEIGHT&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;Problem 1: the length maximum length of variablenew depends on the number of variables: for each variable max 32 chars + 1 for the equals sign. For alphanumeric variables can use the length of the variable. For numerics it is a bit more challenging if no format is assigned to the variable, the using 20 seems to be appropriate.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.class;
   set sashelp.class;
   format Weight commax10.2 Height 5.1;
run;

data _null_;
   set sashelp.vcolumn(where= (Libname = 'WORK' and Memname = 'CLASS')) end=jobDone;
   
   length varList $ 10000;
   retain maxLength 0 regEx varList;
   
   if _n_ = 1 then do;
      regEx = prxparse('/(\d+)\D?/');
   end;
   
   varList = catx(',', VarList, quote(trim(Name)));
   
   if Type = 'char' then do;
      maxLength = maxLength + Length;
   end;
   else do;
      if missing(Format) or not prxmatch(regEx, trim(Format)) then do;
         maxLength = maxLength + 20;
      end;
      else do;
         calcLength = input(prxposn(regEx, 1, Format), best.);
         put name= calcLength= Format=;
         maxLength = maxLength + calcLength;
      end;
   end;
      
   if jobDone then do;
      /* add space for equals sign and blanks */
      maxLength = maxLength + 2 * countw(varList);
      call symputx('maxLength', maxLength);
      call symputx('varList', varList);
   end;
run;

data want;
   set work.class;
   
   length row 8 variablenew $ &amp;amp;maxLength. _variable $ 32;
   
   row = _n_;
   
   do _variable = &amp;amp;varList.;
      variablenew = catx(' ', variablenew, catx('=', _variable, vvaluex(_variable)));
   end;
   
   keep row variablenew;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Oct 2020 07:28:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-rows-into-one-variable/m-p/691151#M210319</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-10-13T07:28:26Z</dc:date>
    </item>
    <item>
      <title>Re: Combine multiple rows into one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-rows-into-one-variable/m-p/691157#M210321</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114470"&gt;@alexkim&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think I would echo&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;sentiments:&amp;nbsp; This doesn't sound like the best idea.&amp;nbsp; I'll show you how to do it, but I'm not sure that it's a good way to do things.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OK, first let's get some test data.&amp;nbsp; If this test data isn't right, then correct it and post it in a reply.&amp;nbsp; Please remember to use the following buttons in your reply window for code and logs:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602575113221.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50591i3933859AC177D3F2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1602575113221.png" alt="jimbarbour_0-1602575113221.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Person_Data;
	INPUT	Variable	:	$25.
			Value		$
			;

datalines;
Person_ID 	1
AGE 25
SEX M
HEIGHT 72
Person_ID 	2
AGE 8
SEX F
HEIGHT 46
Person_ID 	3
AGE 67
SEX M
HEIGHT 68
;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OK, so here's code to do what you want:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Concatenated_Person;
	DROP	Variable
			Value;
	DROP	Person_ID
			Age
			Sex
			Height;

	SET	Person_Data;

	RETAIN	Person_ID
			Age
			Sex
			Height
			;

	IF	UPCASE(Variable)		=	'PERSON_ID'	THEN
		DO;
			Person_Id			=	Value;
			DELETE;
		END;
	ELSE
	IF	UPCASE(Variable)		=	'AGE'	THEN
		DO;
			Age					=	Value;
			DELETE;
		END;
	ELSE
	IF	UPCASE(Variable)		=	'SEX'	THEN
		DO;
			Sex					=	Value;
			DELETE;
		END;
	ELSE
	IF	UPCASE(Variable)		=	'HEIGHT'	THEN
		DO;
			Height				=	Value;
			Variable_New		=	CATX(' ','Person_ID=',Person_ID, 'Age=', Age, 'Sex=',Sex, 'Height=',Height);
			OUTPUT;
			CALL	MISSING(Person_ID, Age, Sex, Height);
			DELETE;
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above code results in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_1-1602575250271.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50592i89B85C552491E08C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_1-1602575250271.png" alt="jimbarbour_1-1602575250271.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might recommend something more along the lines of a program that produces data like this which I think will be a lot more usable:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_3-1602575371548.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50594i603B31AB4EE188BD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_3-1602575371548.png" alt="jimbarbour_3-1602575371548.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a program to produce data like the above:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Consolidated_Person;
	DROP	Variable
			Value;
	SET	Person_Data;

	RETAIN	Person_ID
			Age
			Sex
			Height
			;

	IF	UPCASE(Variable)		=	'PERSON_ID'	THEN
		DO;
			Person_Id			=	Value;
			DELETE;
		END;
	ELSE
	IF	UPCASE(Variable)		=	'AGE'	THEN
		DO;
			Age					=	Value;
			DELETE;
		END;
	ELSE
	IF	UPCASE(Variable)		=	'SEX'	THEN
		DO;
			Sex					=	Value;
			DELETE;
		END;
	ELSE
	IF	UPCASE(Variable)		=	'HEIGHT'	THEN
		DO;
			Height				=	Value;
			OUTPUT;
			CALL	MISSING(Person_ID, Age, Sex, Height);
			DELETE;
		END;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;DIV id="tinyMceEditorjimbarbour_2" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Oct 2020 07:50:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-multiple-rows-into-one-variable/m-p/691157#M210321</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-13T07:50:46Z</dc:date>
    </item>
  </channel>
</rss>

