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

Hi Everyone ,

 

I have a dataset in the below format :

data raw_Data;
length brand $4 status $2 ;
input brand $ status $ Units;
datalines;
Audi A 100
Audi NA 200
Audi NA 20
Audi A 400
Audi A 20
Audi A 30
Merc A 77
Merc A 63
Merc A 48
Merc NA 34
Merc A 19
BMW A 4
BMW NA 400
BMW NA 20
BMW NA 30
BMW A 20
;

 

I want the results as(Sum of units based on status in columns) :

Brand     A    NA
Audi550220
Merc20734
BMW24450

 

could you please help!!!

 

Thanks 🙂 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data raw_Data;
length brand $4 status $2 ;
input brand $ status $ Units;
datalines;
Audi A 100
Audi NA 200
Audi NA 20
Audi A 400
Audi A 20
Audi A 30
Merc A 77
Merc A 63
Merc A 48
Merc NA 34
Merc A 19
BMW A 4
BMW NA 400
BMW NA 20
BMW NA 30
BMW A 20
;

proc sql;
create table want as
select brand,sum((status='A')*units) as A,sum((status='NA')*units) as NA
from raw_Data
group by brand;
quit;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

PROC TABULATE would be a good choice here:

 

proc tabulate data=have;
class brand status;
var units;
tables brand, status=' ' * units=' ' * sum=' ';
run;
ballardw
Super User

@new_sas_user_4 wrote:

Hi Everyone ,

 

I have a dataset in the below format :

data raw_Data;
length brand $4 status $2 ;
input brand $ status $ Units;
datalines;
Audi A 100
Audi NA 200
Audi NA 20
Audi A 400
Audi A 20
Audi A 30
Merc A 77
Merc A 63
Merc A 48
Merc NA 34
Merc A 19
BMW A 4
BMW NA 400
BMW NA 20
BMW NA 30
BMW A 20
;

 

I want the results as(Sum of units based on status in columns) :

Brand      A     NA
Audi 550 220
Merc 207 34
BMW 24 450

 

could you please help!!!

 

Thanks 🙂 

 


Do you want a dataset, used for further computer manipulation, or a report, people read these?

 

Data set: use proc means or summary with brand and status as class variables and sum the units then transpose the data.

 

Report could be as simple as

proc report data=work.raw_data;
   column brand status,units ;
   define brand / group;
   define status/ across ' ';
   define units/ analysis ;
run;

proc tabulate data=work.raw_data;
  class brand status;
  var units;
  table brand=' ',
        status=' '*units=' '* sum=' '*f=best8.
        /box='Brand';
run;
novinosrin
Tourmaline | Level 20
data raw_Data;
length brand $4 status $2 ;
input brand $ status $ Units;
datalines;
Audi A 100
Audi NA 200
Audi NA 20
Audi A 400
Audi A 20
Audi A 30
Merc A 77
Merc A 63
Merc A 48
Merc NA 34
Merc A 19
BMW A 4
BMW NA 400
BMW NA 20
BMW NA 30
BMW A 20
;

proc sql;
create table want as
select brand,sum((status='A')*units) as A,sum((status='NA')*units) as NA
from raw_Data
group by brand;
quit;
new_sas_user_4
Quartz | Level 8

Thank you so much everyone 🙂

 

All solutions work 🙂

I just have other case statements in my query so I suppose the solution by @novinosrin would be the easiest to implement for me !!

 

Thank you again!!

 

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
  • 4 replies
  • 1410 views
  • 1 like
  • 4 in conversation