Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retain and First.var 1

Status
Not open for further replies.

tweaked2005

Technical User
Jun 21, 2005
20
US
I reviewed other post, and still can't seem to come up with a working solution.

I have a dataset similar to below:

id category
1 Fever
1 Headache
1 Nausea
1 Fever
2 Nausea
2 Accident
3 Fever
4 Accident

I want to create a new variable: "conditions" that has a concatenated list of categories by id. So for example, for id = 1, conditions = "FeverHeadacheNauseaFever"

Here is the code I'm using, but I'm not getting the right results:

Code:
data new; set old;
   by id;
   retain id conditions;
   if first.id then conditions = '';
   conditions = compress(conditions||category);
   if last.id then output;

Any help is greatly appreciated. Thanks!
 
Hi,

Try the below:-

Code:
data new;
	set old;
	Length conditions $100.;
	by id;
	retain conditions;
		if first.id then 
		conditions = category;
	else
		conditions = compress(conditions||category);
	if last.id then
		output;
run;

The problem was you were telling it to do something with the first occurrence and the last, but nothing in-between.

So the first occurrence you set the first Category, else you join the category to the retained Condition.. then finally if its the last one. output.

Hope this helps.

Shenn
 
Yay! It worked. Thank you so much! I knew I was close.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top