BookmarkSubscribeRSS Feed
ambadi007
Quartz | Level 8

Dear Experts,

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).

Below the data is available 

 

idVisitresultWanted_Var
100vs1None0-None,1-Severe,2-Moderate,2Moderate
100vs2Severe0-None,1-Severe,2-Moderate,2Moderate
100vs3Moderate0-None,1-Severe,2-Moderate,2Moderate
100vs4Moderate0-None,1-Severe,2-Moderate,2Moderate
101vs1None0-None,1-Severe
101vs2Severe0-None,1-Severe
102vs1Moderate2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe
102vs2Moderate2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe
102vs3Moderate2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe
102vs4Moderate2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe
102vs5Severe2-Moderate,2-Moderate,2-Moderate,2-Moderate,1-Severe
4 REPLIES 4
jimbarbour
Meteorite | Level 14

I think you could do this reasonably easily if you did it in a multi step process along the lines of.  

  1. Create a data step to read in the data.  I might call the data Visit_Detail.
  2. Create an SQL step that does a COUNT(*) with a Group By ID into a macro variable called Max_Visits.  Max_Visits will contain the count of the maximum number of visits for any one ID.
  3. Create another data step that creates a SAS dataset called Visit_Summary.  This data step should have a array with numeric variables Severity1 - Severity&Max_Visits.  A RETAIN statement needs to be coded for Severity1 - Severity&Max_Visits.  As you read, populate the first empty position in the array with the level of severity for the visit (0, 1, or 2).
  4. 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 Severity1 - Severity&Max_Visits and then clear the array.  CALL MISSING(of Severity1 - Severity&Max_Visits) is a great way to clear an array.
  5. Create a final Data step that merges Visit_Detail and Visit_Summary BY ID.  The information in Visit_Summary should be formatted and placed into a variable, Wanted_Var, based on the contents of Severity1 - Severity&Max_Visits.  Variables Severity1 - Severity&Max_Visits should be dropped.

That in a nutshell should give you what you want.  Give it a try.  If you get stuck, post here.  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.

 

Good luck!

 

Jim

ambadi007
Quartz | Level 8

could you please provide me a code for this, then it would be great

jimbarbour
Meteorite | Level 14

I would encourage you to give it a try rather than just copying code; however, that is up to you.  Results and code are below.

 

Jim

 

First, results:

jimbarbour_0-1602426950823.png

Then, code:

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(&Max_Visits));
%PUT	NOTE:  &=Max_Visits;

DATA	Visit_Summary;
	DROP	Visit	Severity _:;
	SET	Visit_Detail;
		BY	ID;
	ARRAY	_Severity	[*]	$32	_Severity1 - _Severity&Max_Visits;
	RETAIN						_Severity1 - _Severity&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	&Max_Visits;
		IF	MISSING(_Severity[_i])	THEN
			DO;
				_Severity[_i]	=	Severity;
				_i				=	&Max_Visits;
			END;
	END;

	IF	LAST.ID	THEN
		DO;
			Severity_Coded		=	CATX(',', OF _Severity1 - _Severity&Max_Visits);
			OUTPUT;
			CALL	MISSING	(of	_Severity1 - _Severity&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;
PGStats
Opal | Level 21

Do this in one step:

 

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;

image.png

PG

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1049 views
  • 0 likes
  • 3 in conversation