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

Hi everyne,

 

Suppose that I have a data like this:

(It shows company A made 4 times transaction to company B, company C made 1 time transaction to company D etc..)

 

A B 4

C D 1

A E 3

 

I need to transform this data like this:

 

 

ID Target

1 A

1 B

2 A

2 B

3 A

3 B

4 A

4 B

---------4 (A,B)

5 C

5 D

---------1 (C,D)

6 A

6 E

7 A

7 E

8 A

8 E

----------3 (A, E)

 

How can I do that?

 

Thank you very much!

 

 

Onur

1 ACCEPTED SOLUTION

Accepted Solutions
MINX
Obsidian | Level 7

Check out. Hope this is what you want

data have;
input col1 :$ col2 :$ n;
datalines;
A B 4
C D 1
A E 3
;
run;

data want;
	set have;
	do id=1 to n;
		target=col1;
		output;
		target=col2;
		output;
	end;
	keep id target;
run;
	

View solution in original post

2 REPLIES 2
MINX
Obsidian | Level 7

Check out. Hope this is what you want

data have;
input col1 :$ col2 :$ n;
datalines;
A B 4
C D 1
A E 3
;
run;

data want;
	set have;
	do id=1 to n;
		target=col1;
		output;
		target=col2;
		output;
	end;
	keep id target;
run;
	
novinosrin
Tourmaline | Level 20
data have;
input var1 $ var2 $ var3;
datalines;
A B 4
C D 1
A E 3
;

data want;
set have;
retain _id;
if _n_=1 then do;
do id =1 to var3;
 target=var1;
 output;
  target=var2;
 output;
end;
_id=id;
end;
else do;
do id= _id by 1 until(id=_id+var3-1);
target=var1;
 output;
  target=var2;
 output;
end;
_id=id+1;
end;
keep id target;
run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 382 views
  • 0 likes
  • 3 in conversation