BookmarkSubscribeRSS Feed
sas_cc
Calcite | Level 5

Hi SAS Experts,

 

The data I have now is:

 

Name      Measurement        Jan         Feb        Mar       Apr     May

a                 z                         #             # ...

a                 y                          #   ...

a                 x                           ...

b                 z

b                 y

b                 x

 

 

My code is:

PROC REPORT NOWD;

 

COLUMN name measurement jan feb ...;

DEFINE name ...;

DEFINE measurement ....;

...;

BREAK BEFORE name / SUMMARIZE...;

 

As a result, I get the SUM for each column based on the name. That is, 

 

Name        Measurement       Jan          Feb        Mar       Apr     May  

a                                            Sum       Sum ....

a                 z                          #             # ...

a                 y                          #   ...

a                 x                           ...

b                                            Sum       Sum ....

b                 z                          #             # ...

b                 y                          #   ...

b                 x                           ...

 

 

 

How do I tweak the BREAK statement so I can compute the average for the NAME a, and sum for the NAME b, that is:

 

Name        Measurement       Jan          Feb        Mar       Apr     May  

a                                            Average    Average ....

a                 z                          #             # ...

a                 y                          #   ...

a                 x                           ...

b                                            Sum       Sum ....

b                 z                          #             # ...

b                 y                          #   ...

b                 x                           ...

 

Thank you very much!

1 REPLY 1
BrunoMueller
SAS Super FREQ

Please explain the use case for this, seems to be a strange report.

 

You can do this quite well using Proc REPORT and a compute block. You will need to have all the month columns twice, once for the SUM and once for the MEAN.

 

You can then check in which line of the report you are and fill the cells accordingly.

 

The code below uses the duplicate month columns with different statistics and a compute block to assign the proper value depending in which line you are the _BREAK_ variable tells you when you are on a break line. The _dummy variable shows the values.

 

 

 

data have;
  length name measure $ 8;
  array months{*} jan feb;
  do name = "a", "b";
    do measure = "x", "y", "z";
      do j = 1 to 5;
      do i = 1 to dim(months);
        months{i} = ceil(ranuni(123) * 100);
      end;
      output;
      end;
    end;
  end;
run;

/*%let doNotPrint = noprint;*/
%let doNotPrint = ;

proc report data=have;
  column name measure jan=jan_sum jan=jan_mean feb=feb_sum feb=feb_mean _dummy;
  define name / group;
  define measure / group;
  define jan_sum / analysis sum "jan" format=comma12.2;
  define jan_mean / analysis mean "jan mean" &doNotPrint;
  define feb_sum / analysis sum "feb" format=comma12.2;
  define feb_mean / analysis mean "feb mean" &doNotPrint;
  define _dummy / computed &doNotPrint;

  break before name / summarize ;

  compute _dummy / char length=32;
    _dummy = catx(":", name, _break_);

    if name = "a" and _break_ = "name" then do;
      jan_sum = jan_mean;
      feb_sum = feb_mean;
    end;
  endcomp;
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
  • 1 reply
  • 577 views
  • 0 likes
  • 2 in conversation