<?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: Need to get the result value horizontally based on the visit variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690793#M210176</link>
    <description>&lt;P&gt;I would encourage you to give it a try rather than just copying code; however, that is up to you.&amp;nbsp; Results and code are below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602426950823.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50547iA79B861A3C66978D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1602426950823.png" alt="jimbarbour_0-1602426950823.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Then, code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Visit_Detail;
	INFILE	DATALINES	MISSOVER;
	INPUT	ID	
			Visit		$	
			Severity	:	$32.;
DATALINES;
100 vs1 None
100 vs2 Severe
100 vs3 Moderate
100 vs4 Moderate
101 vs1 None    
101 vs2 Severe
102 vs1 Moderate
102 vs2 Moderate
102 vs3 Moderate
102 vs4 Moderate
102 vs5 Severe
;
RUN;

PROC	SQL;
	SELECT	MAX(Visits)	INTO		:	Max_Visits
		FROM	(SELECT	COUNT(1)	AS	Visits
				FROM	Visit_Detail
				GROUP	BY	ID)
				;
QUIT;

%LET	Max_Visits	=	%SYSFUNC(STRIP(&amp;amp;Max_Visits));
%PUT	NOTE:  &amp;amp;=Max_Visits;

DATA	Visit_Summary;
	DROP	Visit	Severity _:;
	SET	Visit_Detail;
		BY	ID;
	ARRAY	_Severity	[*]	$32	_Severity1 - _Severity&amp;amp;Max_Visits;
	RETAIN						_Severity1 - _Severity&amp;amp;Max_Visits;

	SELECT	(PROPCASE(Severity));
		WHEN	("Moderate")
			Severity			=	"2-Moderate";
		WHEN	("Severe")
			Severity			=	"1-Severe";
		WHEN	("None")
			Severity			=	"0-None";
	END;

	DO	_i	=	1	TO	&amp;amp;Max_Visits;
		IF	MISSING(_Severity[_i])	THEN
			DO;
				_Severity[_i]	=	Severity;
				_i				=	&amp;amp;Max_Visits;
			END;
	END;

	IF	LAST.ID	THEN
		DO;
			Severity_Coded		=	CATX(',', OF _Severity1 - _Severity&amp;amp;Max_Visits);
			OUTPUT;
			CALL	MISSING	(of	_Severity1 - _Severity&amp;amp;Max_Visits);
		END;
RUN;

PROC	SQL;
	CREATE	TABLE	Final_Visits		AS
	SELECT	D.*	
			,S.*
		FROM	Visit_Detail			D
		INNER	JOIN	Visit_Summary	S
			ON	D.ID	=	S.ID
			;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 11 Oct 2020 14:37:22 GMT</pubDate>
    <dc:creator>jimbarbour</dc:creator>
    <dc:date>2020-10-11T14:37:22Z</dc:date>
    <item>
      <title>Need to get the result value horizontally based on the visit variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690679#M210127</link>
      <description>&lt;P&gt;Dear Experts,&lt;/P&gt;&lt;P&gt;I need a help based on the data i provided. I need to create a new variable (in data i named as Wanted_Var) based on the visits . Need to to concatenate the result variable with a comma horizontally based on the visits for a particular id ( need the complete result to concatenate for a particular id for all the visits.) , The Wanted Variable should have added a numeric value also ( ie if its None then it should be like 0-None, or Severe it should like 1-Severe).&lt;/P&gt;&lt;P&gt;Below the data is available&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;Visit&lt;/TD&gt;&lt;TD&gt;result&lt;/TD&gt;&lt;TD&gt;Wanted_Var&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;vs1&lt;/TD&gt;&lt;TD&gt;None&lt;/TD&gt;&lt;TD&gt;0-None,1-Severe,2-Moderate,2Moderate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;vs2&lt;/TD&gt;&lt;TD&gt;Severe&lt;/TD&gt;&lt;TD&gt;0-None,1-Severe,2-Moderate,2Moderate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;vs3&lt;/TD&gt;&lt;TD&gt;Moderate&lt;/TD&gt;&lt;TD&gt;0-None,1-Severe,2-Moderate,2Moderate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;vs4&lt;/TD&gt;&lt;TD&gt;Moderate&lt;/TD&gt;&lt;TD&gt;0-None,1-Severe,2-Moderate,2Moderate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;vs1&lt;/TD&gt;&lt;TD&gt;None&lt;/TD&gt;&lt;TD&gt;0-None,1-Severe&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;101&lt;/TD&gt;&lt;TD&gt;vs2&lt;/TD&gt;&lt;TD&gt;Severe&lt;/TD&gt;&lt;TD&gt;0-None,1-Severe&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;TD&gt;vs1&lt;/TD&gt;&lt;TD&gt;Moderate&lt;/TD&gt;&lt;TD&gt;2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;TD&gt;vs2&lt;/TD&gt;&lt;TD&gt;Moderate&lt;/TD&gt;&lt;TD&gt;2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;TD&gt;vs3&lt;/TD&gt;&lt;TD&gt;Moderate&lt;/TD&gt;&lt;TD&gt;2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;TD&gt;vs4&lt;/TD&gt;&lt;TD&gt;Moderate&lt;/TD&gt;&lt;TD&gt;2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;TD&gt;vs5&lt;/TD&gt;&lt;TD&gt;Severe&lt;/TD&gt;&lt;TD&gt;2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Sat, 10 Oct 2020 15:10:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690679#M210127</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2020-10-10T15:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: Need to get the result value horizontally based on the visit variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690691#M210132</link>
      <description>&lt;P&gt;I think you could do this reasonably easily if you did it in a multi step process along the lines of.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a data step to read in the data.&amp;nbsp; I might call the data Visit_Detail.&lt;/LI&gt;
&lt;LI&gt;Create an SQL step that does a COUNT(*) with a Group By ID into a macro variable called Max_Visits.&amp;nbsp; Max_Visits will contain the count of the maximum number of visits for any one ID.&lt;/LI&gt;
&lt;LI&gt;Create another data step that creates a SAS dataset called Visit_Summary.&amp;nbsp; This data step should have a&amp;nbsp;array with numeric variables Severity1 - Severity&amp;amp;Max_Visits.&amp;nbsp; A&amp;nbsp;RETAIN statement needs to be coded for&amp;nbsp;Severity1 - Severity&amp;amp;Max_Visits.&amp;nbsp; As you read, populate the first empty position in the array with the level of severity for the visit (0, 1, or 2).&lt;/LI&gt;
&lt;LI&gt;In the Data step in #3, above, when the ID changes from one person to the next, write out a record containing the ID and&amp;nbsp;Severity1 - Severity&amp;amp;Max_Visits and then clear the array.&amp;nbsp; CALL MISSING(of Severity1 - Severity&amp;amp;Max_Visits) is a great way to clear an array.&lt;/LI&gt;
&lt;LI&gt;Create a final Data step that merges Visit_Detail and Visit_Summary BY ID.&amp;nbsp; The information in Visit_Summary should be formatted and placed into a variable, Wanted_Var, based on the contents of&amp;nbsp;Severity1 - Severity&amp;amp;Max_Visits.&amp;nbsp; Variables&amp;nbsp;Severity1 - Severity&amp;amp;Max_Visits should be dropped.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;That in a nutshell should give you what you want.&amp;nbsp; Give it a try.&amp;nbsp; If you get stuck, post here.&amp;nbsp; I'm headed out (it's Saturday here), but I'll be back in a few hours, and I we can talk through things if you get stuck.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Sat, 10 Oct 2020 16:48:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690691#M210132</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-10T16:48:51Z</dc:date>
    </item>
    <item>
      <title>Re: Need to get the result value horizontally based on the visit variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690703#M210137</link>
      <description>&lt;P&gt;Do this in one step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id  Visit $  result $;
datalines;
100 vs1 None
100 vs2 Severe
100 vs3 Moderate
100 vs4 Moderate
101 vs1 None    
101 vs2 Severe
102 vs1 Moderate
102 vs2 Moderate
102 vs3 Moderate
102 vs4 Moderate
102 vs5 Severe
;

data want;
do until(last.id);
    set have; by id;
    length list $100;
    list = catx(",", list, catx("-", whichc(result,"None", "Severe", "Moderate")-1, result));
    end;
do until(last.id);
    set have; by id;
    output;
    end;
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 413px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50534i83B208012C7D75EE/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Oct 2020 19:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690703#M210137</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-10-10T19:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: Need to get the result value horizontally based on the visit variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690753#M210156</link>
      <description>&lt;P&gt;could you please provide me a code for this, then it would be great&lt;/P&gt;</description>
      <pubDate>Sun, 11 Oct 2020 03:20:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690753#M210156</guid>
      <dc:creator>ambadi007</dc:creator>
      <dc:date>2020-10-11T03:20:03Z</dc:date>
    </item>
    <item>
      <title>Re: Need to get the result value horizontally based on the visit variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690793#M210176</link>
      <description>&lt;P&gt;I would encourage you to give it a try rather than just copying code; however, that is up to you.&amp;nbsp; Results and code are below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1602426950823.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50547iA79B861A3C66978D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_0-1602426950823.png" alt="jimbarbour_0-1602426950823.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Then, code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Visit_Detail;
	INFILE	DATALINES	MISSOVER;
	INPUT	ID	
			Visit		$	
			Severity	:	$32.;
DATALINES;
100 vs1 None
100 vs2 Severe
100 vs3 Moderate
100 vs4 Moderate
101 vs1 None    
101 vs2 Severe
102 vs1 Moderate
102 vs2 Moderate
102 vs3 Moderate
102 vs4 Moderate
102 vs5 Severe
;
RUN;

PROC	SQL;
	SELECT	MAX(Visits)	INTO		:	Max_Visits
		FROM	(SELECT	COUNT(1)	AS	Visits
				FROM	Visit_Detail
				GROUP	BY	ID)
				;
QUIT;

%LET	Max_Visits	=	%SYSFUNC(STRIP(&amp;amp;Max_Visits));
%PUT	NOTE:  &amp;amp;=Max_Visits;

DATA	Visit_Summary;
	DROP	Visit	Severity _:;
	SET	Visit_Detail;
		BY	ID;
	ARRAY	_Severity	[*]	$32	_Severity1 - _Severity&amp;amp;Max_Visits;
	RETAIN						_Severity1 - _Severity&amp;amp;Max_Visits;

	SELECT	(PROPCASE(Severity));
		WHEN	("Moderate")
			Severity			=	"2-Moderate";
		WHEN	("Severe")
			Severity			=	"1-Severe";
		WHEN	("None")
			Severity			=	"0-None";
	END;

	DO	_i	=	1	TO	&amp;amp;Max_Visits;
		IF	MISSING(_Severity[_i])	THEN
			DO;
				_Severity[_i]	=	Severity;
				_i				=	&amp;amp;Max_Visits;
			END;
	END;

	IF	LAST.ID	THEN
		DO;
			Severity_Coded		=	CATX(',', OF _Severity1 - _Severity&amp;amp;Max_Visits);
			OUTPUT;
			CALL	MISSING	(of	_Severity1 - _Severity&amp;amp;Max_Visits);
		END;
RUN;

PROC	SQL;
	CREATE	TABLE	Final_Visits		AS
	SELECT	D.*	
			,S.*
		FROM	Visit_Detail			D
		INNER	JOIN	Visit_Summary	S
			ON	D.ID	=	S.ID
			;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 Oct 2020 14:37:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-get-the-result-value-horizontally-based-on-the-visit/m-p/690793#M210176</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-11T14:37:22Z</dc:date>
    </item>
  </channel>
</rss>

