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

data have;
input fruit$ fru$ fco;
datalines;
banan f 10
broc v 14
apple f 15
;
run;
proc sort data = have; by fruit ;
proc transpose data = have out = fruit1;
by fruit ;
var fco ;
id fru ;
run;

now i need to transpose it back to get the original data as it was.

1 ACCEPTED SOLUTION

Accepted Solutions
hhinohar
Quartz | Level 8

Sorry for misreading the intention.

I tried a few option and I guess below is what is possible with proc transpose.
First I tried proc transpose with var and by option but that generated n*n obs with fruit and fco group
which led to outputting unnecessary missing observations.

So I added where= data set option that omits missing when outputting result data set and that generated the desired output.

I hope this helps!

 

data have;
input fruit$ fru$ fco;
datalines;
banan f 10
broc v 14
apple f 15
;
run;
proc sort data = have; by fruit fru;
proc transpose data = have out = fruit1;
	by fruit ;
	var fco ;
	id fru ;
run;

/* omit missing with where= dataset option  */
/* as transpose will output missing obs with n*n merge (fruit fco)*/
proc transpose data = fruit1 out = want(rename=(_name_=fru) where=(fco ne .));
	var f v;
	by fruit;
run;

View solution in original post

7 REPLIES 7
hhinohar
Quartz | Level 8

Here is a quick sample.

data want(keep=fruit fru fco);
	set fruit1;
/* 	put sas variables into array */
	array tmp[2] _numeric_;
/* 	loop through array and put everything back into place */
	do i=1 to dim(tmp);
		fru=vlabel(tmp[i]);
		fco=tmp[i];
		if tmp[i] ne . then	output;
	end;
run;
helpmedoubts
Fluorite | Level 6

Thank you. But i need the output using proc transpose only

Patrick
Opal | Level 21

@helpmedoubts wrote:

Thank you. But i need the output using proc transpose only


Sounds like an assignment problem. What have you tried already and where do you get stuck?

helpmedoubts
Fluorite | Level 6

Just trying to understand the concept in all ways. When I tried I was getting duplicates as in instead of 3 obs as output i was getting 6obs

hhinohar
Quartz | Level 8

Sorry for misreading the intention.

I tried a few option and I guess below is what is possible with proc transpose.
First I tried proc transpose with var and by option but that generated n*n obs with fruit and fco group
which led to outputting unnecessary missing observations.

So I added where= data set option that omits missing when outputting result data set and that generated the desired output.

I hope this helps!

 

data have;
input fruit$ fru$ fco;
datalines;
banan f 10
broc v 14
apple f 15
;
run;
proc sort data = have; by fruit fru;
proc transpose data = have out = fruit1;
	by fruit ;
	var fco ;
	id fru ;
run;

/* omit missing with where= dataset option  */
/* as transpose will output missing obs with n*n merge (fruit fco)*/
proc transpose data = fruit1 out = want(rename=(_name_=fru) where=(fco ne .));
	var f v;
	by fruit;
run;
helpmedoubts
Fluorite | Level 6

Thank you. Both the programs worked. But i was looking for the second program(transpose). Sorry for the confusion. was getting duplicates in output, didn't think of using where option. Thanks again.

hhinohar
Quartz | Level 8

my pleasure.

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
  • 2268 views
  • 0 likes
  • 3 in conversation