BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sagulolo
Quartz | Level 8

Dear All,

 

I got a dataset as per below:-

 

group_noitem_namecompany
020101banana 
020101apple 
020101grape 
  Company A
020101apple 
020101pineapple 
  Company B
020102pineapple 
020102apple 
020102grape 
020102papaya 
  Company C

 

How to transform it into below dataset? Just want the company to be first row instead of last row? 

The no of items can be any number.

 

group_noitem_namecompany
020101bananaCompany A
020101apple 
020101grape 
020101appleCompany B
020101pineapple 
020102pineappleCompany C
020102apple 
020102grape 
020102papaya 

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

data have;
	infile cards dlm=',' dsd;
	format group_no z6. item_name $10. company $10.;
	input group_no item_name company;

	cards;
020101,banana, 
020101,apple, 
020101,grape, 
., ,Company A
020101,apple, 
020101,pineapple, 
., ,Company B
020102,pineapple, 
020102,apple, 
020102,grape, 
020102,papaya, 
., ,Company C
;
run;


data have2(keep=group_no item_name compid) companies(keep=company compid);
	set have;

	retain compid;

	no1=lag(group_no);

	if _N_=1 or no1=. then do;
		compid+1;
	end;

	if group_no=. then output companies;
	else output have2;
run;

data want;
	merge have2 companies;
	by compid;

	comp1=lag(company);

	if company=comp1 then company=" ";

	drop compid comp1;
run;

View solution in original post

7 REPLIES 7
gamotte
Rhodochrosite | Level 12

Hello,

 

data have;
	infile cards dlm=',' dsd;
	format group_no z6. item_name $10. company $10.;
	input group_no item_name company;

	cards;
020101,banana, 
020101,apple, 
020101,grape, 
., ,Company A
020101,apple, 
020101,pineapple, 
., ,Company B
020102,pineapple, 
020102,apple, 
020102,grape, 
020102,papaya, 
., ,Company C
;
run;


data have2(keep=group_no item_name compid) companies(keep=company compid);
	set have;

	retain compid;

	no1=lag(group_no);

	if _N_=1 or no1=. then do;
		compid+1;
	end;

	if group_no=. then output companies;
	else output have2;
run;

data want;
	merge have2 companies;
	by compid;

	comp1=lag(company);

	if company=comp1 then company=" ";

	drop compid comp1;
run;
sagulolo
Quartz | Level 8
Hi gamotte,

Thank you so much, it help me a lot and it totally out of my mind, it can be done by this way.

Best regards and thank for your time
Ksharp
Super User
data have;
	infile cards dlm=',' dsd;
	format group_no z6. item_name $10. company $10.;
	input group_no item_name company;

	cards;
020101,banana, 
020101,apple, 
020101,grape, 
., ,Company A
020101,apple, 
020101,pineapple, 
., ,Company B
020102,pineapple, 
020102,apple, 
020102,grape, 
020102,papaya, 
., ,Company C
;
run;
data key;
 set have;
 if not missing(company);
 keep company;
run;
data want;
 set have;
 call missing(company);
 if missing(lag(group_no)) then set key;
 if not missing(group_no);
run;
sagulolo
Quartz | Level 8
Hi Ksharp,

Thank for your code.

appreciated if you could explain more on part below:-

"if missing(lag(group_no)) then set key;"

thanks in advance
Ksharp
Super User

That means if lag value of group_no is missing ,then copy an obs/record from table KEY.

Note : it copy obs from KEY is one by one until the end of KEY .

mkeintz
PROC Star

There are reasonable single data step solutions:

 

data have;
	infile cards dlm=',' dsd;
	format group_no z6. item_name $10. company $10.;
	input group_no item_name company;

	cards;
020101,banana, 
020101,apple, 
020101,grape, 
., ,Company A
020101,apple, 
020101,pineapple, 
., ,Company B
020102,pineapple, 
020102,apple, 
020102,grape, 
020102,papaya, 
., ,Company C
;
run;

data want (drop=_:);
  set have;

  if _n_=1 or group_no=. then 
    set have (keep=company rename=(company=_ref_value) where=(_ref_value^=''));

  if group_no =. then delete;
  else if _ref_value^=lag(_ref_value) then company=_ref_value;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
sagulolo
Quartz | Level 8

Hi mkeintz,

 

Thank for your code and help.

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 7 replies
  • 2228 views
  • 4 likes
  • 4 in conversation