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

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
Obsidian | Level 7

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!!

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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