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

I have the following data and query

data have;
   informat date date9. quantity 2.;
   input part $ date quantity;
   format date date9. quantity 2.;
   datalines;

A 01jan2013 10
A 01JUN2013 5
A 12DEC2013 4
A 01FEB2014 6
A 15JUN2014 20
B 20FEB2013 5
B 15JAN2013 6
;

proc sql;
    create table almostwant as
    select part, year(date) as year, sum(quantity) as quantity
        from have
    group by part, year
    ;

quit;

I would like to output this data so that each year is created as a new variable rather then in a list format.  So i would like to see the data look like below but i cant seem to get it to work.  Any suggestions would be greatly appreciated.

Want:

Part     2013qty      2014qty

A               19          26

B               11          .

1 ACCEPTED SOLUTION

Accepted Solutions
CTorres
Quartz | Level 8

Complete the solution with Proc TRANSPOSE:

proc transpose data=almostwant out=want(drop=_name_) prefix=QTY;

  by part;

  id year;

  var quantity;

quit;

Regards,

View solution in original post

4 REPLIES 4
CTorres
Quartz | Level 8

Complete the solution with Proc TRANSPOSE:

proc transpose data=almostwant out=want(drop=_name_) prefix=QTY;

  by part;

  id year;

  var quantity;

quit;

Regards,

slchen
Lapis Lazuli | Level 10


proc sql;
select part, max(_2013qty) as _2013qty,max(_2014qty) as _2014qty

from (

         select part, ifn(year(date)=2013,sum(quantity),0) as _2013qty,
               ifn(year(date)=2014, sum(quantity),0) as _2014qty
          from have group by part,year(date)

        ) group by part;
quit;

new4new
Calcite | Level 5

Hi dsbihill,

First of all you cannot have SAS variable names starting with numbers. So your ‘Want:’ example shows labels and not variable names.

It is a standard proc transpose task:

data have;

   informat date date9. quantity 2.;

   input part $ date quantity;

   format date date9. quantity 2.;

   datalines;

A 01jan2013 10

A 01JUN2013 5

A 12DEC2013 4

A 01FEB2014 6

A 15JUN2014 20

B 20FEB2013 5

B 15JAN2013 6

;

proc sql;

    create table almostwant as

    select part, cats(put(year(date), best4.), 'qty') as year lenght=8, sum(quantity) as quantity

        from have

    group by part, year

   order by 1, 2 ;

quit;

proc transpose data=almostwant

out=want(drop=quantity)

name=quantity;

by part;

id year;

idlabel year;

run;

stat_sas
Ammonite | Level 13

data want;
set have;
year=year(date);
run;

proc tabulate data=want;
class part year;
var quantity;
table part,year=' '*quantity*sum=' '*f=8.0;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 865 views
  • 3 likes
  • 5 in conversation