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.
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;
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;
Thank you. But i need the output using proc transpose only
@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?
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
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;
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.
my pleasure.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: