BookmarkSubscribeRSS Feed
ywon111
Quartz | Level 8

Hi, hoping to get some help to get from this:

 

data have;
input ID Type $ Cost AppleValue OrangeValue;
infile datalines missover;
datalines;
1 Apple 810 1 .
1 Orange 2000 . 3
;

To this is what I need:

ID Count_Apple Count_Orange AppleValueA OrangeValueA
1 1 3 810 2000

 

Thanks

2 REPLIES 2
Tom
Super User Tom
Super User

Does your real data have the data only on the diagonal like that? 

data have;
   input ID Type $ Cost Apple Orange Pear Zucchini ;
datalines;
1 Apple      810 1 . . .
1 Orange    2000 . 3 . .
1 Pear       300 . . 4 .
1 Zucchini   400 . . . 5
;

Or are some of the off diagonal cells non-missing? If so where are those values supposed to end up in the target dataset?

So perhaps you should first make the data taller and then transpose it.

data for_transpose;
  set have;
  by id ;
  row+first.id;
  length var $32 ;
  var = cats('Count',type);
  value = max(of apple -- zucchini);
  output;
  var = cats(type,'Value');
  value = cost;
  output;
run;

proc transpose data=for_transpose out=want;
  by id ;
  id var row;
  var value;
run;

Result

Tom_0-1653402901137.png

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 742 views
  • 1 like
  • 3 in conversation