DATA Step, Macro, Functions and more

Duplicate rows

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

Duplicate rows

[ Edited ]

Hi all,

 

I am new here, and very newbie on SAS, so forgive me by advance for the easy question I will ask.

 

I have a table with several rows and several data such as

- a text named "TE1"

- three amounts named "AM1", "AM2", "AM3"

 

For each row, there is at least one of the three amounts not null, but for some rows two of them can be not null of even all three of them can be not null.

 

What I want to do is to create a new table with a program duplicating every row where at least two of the three amounts are not null, in order to have only one amount not null

 

Exemple of table

TE1 AM1 AM2 AM3

AZZ 12 0 1

ABB 15 0 0

AFD 12 8 5

 

I want to have in my new table

TE1 AM1 AM2 AM3

AZZ 12 0 0

AZZ 0 0 1

ABB 15 0 0

AFD 12 0 0

AFD 0 8 0

AFD 0 0 5

 

Tell me if it is not enough clear.

I have truly no ideas of how to do it. Can you help me?

 

Thanks

 


Accepted Solutions
Solution
‎12-28-2016 07:17 AM
Super Contributor
Posts: 305

Re: Duplicate rows

Hello,

 

data want;
set have;

array no{*} AM1 AM2 AM3;
array no_new{*} AM1_ AM2_ AM3_;

do i=1 to dim(no);
 if no{i} ne 0 then 
	do;
	  do j=1 to dim(no);
	  	if j=i then no_new{j}=no{i};
		else  no_new{j}=0;
	  end;
      output;
	end;
end;

drop AM1-Am3 i j;

rename 
am1_=am1
am2_=am2
am3_=am3;
run;

View solution in original post


All Replies
Solution
‎12-28-2016 07:17 AM
Super Contributor
Posts: 305

Re: Duplicate rows

Hello,

 

data want;
set have;

array no{*} AM1 AM2 AM3;
array no_new{*} AM1_ AM2_ AM3_;

do i=1 to dim(no);
 if no{i} ne 0 then 
	do;
	  do j=1 to dim(no);
	  	if j=i then no_new{j}=no{i};
		else  no_new{j}=0;
	  end;
      output;
	end;
end;

drop AM1-Am3 i j;

rename 
am1_=am1
am2_=am2
am3_=am3;
run;
Contributor
Posts: 45

Re: Duplicate rows

Exactly what I needeed!

 

Thank you very much for the fast answer, and the efficiancy!

Happy new year 2017!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 166 views
  • 0 likes
  • 2 in conversation