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


Hi Team,

This is from the SAS Documentation on Building Proc Report.

I know If I have a variable A and I am writing B=A then the new variableB will hold exactly all the values of A;

Below in the proe report Column statement he is writing sales=salespct

and he is using pctsum in the define statement without analysis option as shown below for salespct variable.

define salespct   / pctsum format=percent9.2;

Then He gets the variable named sales.pctsum in the PDV

Could you please help me understand this concept...

Great Help

proc report data=grocery noheader nowindows;
  column sector department sales
         Sctrpct sales=Salespct;

  define sector     / 'Sector' group
                       format=$sctrfmt.;
  define department / group format=$deptfmt.;
  define sales      / analysis sum
                      format=dollar9.2 ;
  define sctrpct    / computed
                      format=percent9.2 ;
  define salespct   / pctsum format=percent9.2;

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  In the PROC REPORT documentation, you should find the concept of defining an alias. When you declare a new report item to be created from an existing report item, you are creating a report item called an ALIAS. Usually this is done because you explicitly want to get a different statistic from PROC REPORT. For example in the program below:

 

proc report data=sashelp.shoes nowd;

  column region product sales sales=salesmn sales=salesmx sales=salesmed;

  define region / group;

  define product / group;

  define sales / analysis sum 'Total Sales';

  define salesmn / analysis min 'Min Sales';

  define salesmx / analysis max 'Max Sales';

  define salesmed / analysis median 'Median Sales';

run;

  The variable SALES is used 4 times, total on the report. For one usage, SALES is an analysis variable with the SUM statistic, for another, SALESMN is the 2nd usage of SALES, only this time, with the MIN statistic; then, the 3rd time, SALESMX is the alias for SALES with the MAX statistic; and the 4th time, SALESMED is the alias of SALES used with the MEDIAN statistic. In the above code, the usage of "ANALYSIS" is not required on each DEFINE statement. With numeric variables, a statistic on the DEFINE statement automatically implies the USAGE of ANALYSIS. So you can, but do not need to have the ANALYSIS on each DEFINE. These are equivalent statements:

  define sales / analysis sum 'Total Sales';

OR

  define sales / sum 'Total Sales';

   

  define salesmn / analysis min 'Min Sales';

OR

  define salesmn / min 'Min Sales';

    So, to your first question, ANALYSIS is implied with the statistic PCTSUM, so it is not REQUIRED on the DEFINE statement.

    Next, PROC REPORT does NOT have a Program Data Vector (PDV) -- only the DATA step has a PDV. Since you did not show a complete PROC REPORT program with a COMPUTE block (or you only copied part of the example program), I'm not sure what your question is about the COMPOUND name. The fact is that there is only 1 variable in my program above that would have or need a compound name. If I was going to refer to the SALES variable, the correct compound name would be SALES.SUM. The other items that come from using an alias, SALESMN, SALESMX, and SALESMED do NOT need a compound name -- in fact, PROC REPORT won't let you use a COMPOUND name for these aliases (see below). In a COMPUTE block, these items can be referenced by the simple alias name (without the statistic). This documentation page explicitly shows that in Example 3, described here:

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n0ldy84v9j7463n1jjklo... , you can note how SALESMIN and SALESMAX are used in a COMPUTE block without a compound name.

  The program that you ask about is from the documentation section entitled (How PROC REPORT Builds a Report, subtopic: Building a Report That Uses Temporary Variables):

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n1gb1fq6jc5syxn162qoa... and in the example the "concept" of SALES.PCTSUM is introduced to show how statistics are calculated and held in a memory location (note that the term "PDV" is NOT ever used in the documentation topic). Conceptually, the documentation topic discusses SALES.SUM and SALES.PCTSUM as being available on the COMPUTE before -- but what they mean is that in a pre-processing phase, all the statistics based on SALES are calculated. However, you never see SALES.PCTSUM used in the program and if you DID use SALES.PCTSUM in the program, you would get an error message in the log. Consider the log message that my program gets if I try to use a reference like SALES.PCTSUM for an alias in my program:

88   ods html file='c:\temp\test_compound.html' style=sasweb;
NOTE: Writing HTML Body file: c:\temp\test_compound.html
89   proc report data=sashelp.shoes nowd;
90     where region in ('United States', 'Canada');
91     column region product sales sales=salespct newvar1 newvar2;
92     define region / group;
93     define product / group;
94     define sales / sum 'Total Sales';
95     define salespct / pctsum 'Pctsum Sales' f=10.6;
96     define newvar1 / computed f=10.6;
97     define newvar2 / computed f=10.6;
98     compute newvar1;
99       newvar1 = salespct * 100;
100    endcomp;
101    compute newvar2;
102      newvar2=sales.pctsum * 100;
103    endcomp;
104  run;

ERROR: The variable type of SALES.PCTSUM is invalid in this context.
NOTE: The preceding messages refer to the COMPUTE block for newvar2.
NOTE: Will not run due to compilation errors.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 77 observations read from the data set SASHELP.SHOES.
      WHERE region in ('Canada', 'United States');
NOTE: PROCEDURE REPORT used (Total process time):
      real time           0.08 seconds
      cpu time            0.03 seconds

105  ods _all_ close;

  As you can see...SALES.PCTSUM is an invalid reference in the COMPUTE block. You MUST use the ALIAS name as the reference in the COMPUTE block. The reference to SALES.PCTSUM in the documentation topic you cited is only a conceptual reference. In order for the above program to work, the assignment statement for NEWVAR2 would have to be the same as the assignment statement for NEWVAR1 in order to eliminate the error message. In the documentation topic, Four Ways to Reference Report Items in a Compute Block,  (http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n0jann0dticnpbn1woe95...), you will find a table that shows exactly how and when you use a compound name versus a "regular" or simple name. Under the ANALYSIS usage, it says that you use a COMPOUND name except (note the * comment) that "If the variable has an alias, then you must reference it with the alias."

    I hope this explains the documentation example in a bit more detail.

cynthia

.

View solution in original post

2 REPLIES 2
Cynthia_sas
SAS Super FREQ

Hi:

  In the PROC REPORT documentation, you should find the concept of defining an alias. When you declare a new report item to be created from an existing report item, you are creating a report item called an ALIAS. Usually this is done because you explicitly want to get a different statistic from PROC REPORT. For example in the program below:

 

proc report data=sashelp.shoes nowd;

  column region product sales sales=salesmn sales=salesmx sales=salesmed;

  define region / group;

  define product / group;

  define sales / analysis sum 'Total Sales';

  define salesmn / analysis min 'Min Sales';

  define salesmx / analysis max 'Max Sales';

  define salesmed / analysis median 'Median Sales';

run;

  The variable SALES is used 4 times, total on the report. For one usage, SALES is an analysis variable with the SUM statistic, for another, SALESMN is the 2nd usage of SALES, only this time, with the MIN statistic; then, the 3rd time, SALESMX is the alias for SALES with the MAX statistic; and the 4th time, SALESMED is the alias of SALES used with the MEDIAN statistic. In the above code, the usage of "ANALYSIS" is not required on each DEFINE statement. With numeric variables, a statistic on the DEFINE statement automatically implies the USAGE of ANALYSIS. So you can, but do not need to have the ANALYSIS on each DEFINE. These are equivalent statements:

  define sales / analysis sum 'Total Sales';

OR

  define sales / sum 'Total Sales';

   

  define salesmn / analysis min 'Min Sales';

OR

  define salesmn / min 'Min Sales';

    So, to your first question, ANALYSIS is implied with the statistic PCTSUM, so it is not REQUIRED on the DEFINE statement.

    Next, PROC REPORT does NOT have a Program Data Vector (PDV) -- only the DATA step has a PDV. Since you did not show a complete PROC REPORT program with a COMPUTE block (or you only copied part of the example program), I'm not sure what your question is about the COMPOUND name. The fact is that there is only 1 variable in my program above that would have or need a compound name. If I was going to refer to the SALES variable, the correct compound name would be SALES.SUM. The other items that come from using an alias, SALESMN, SALESMX, and SALESMED do NOT need a compound name -- in fact, PROC REPORT won't let you use a COMPOUND name for these aliases (see below). In a COMPUTE block, these items can be referenced by the simple alias name (without the statistic). This documentation page explicitly shows that in Example 3, described here:

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n0ldy84v9j7463n1jjklo... , you can note how SALESMIN and SALESMAX are used in a COMPUTE block without a compound name.

  The program that you ask about is from the documentation section entitled (How PROC REPORT Builds a Report, subtopic: Building a Report That Uses Temporary Variables):

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n1gb1fq6jc5syxn162qoa... and in the example the "concept" of SALES.PCTSUM is introduced to show how statistics are calculated and held in a memory location (note that the term "PDV" is NOT ever used in the documentation topic). Conceptually, the documentation topic discusses SALES.SUM and SALES.PCTSUM as being available on the COMPUTE before -- but what they mean is that in a pre-processing phase, all the statistics based on SALES are calculated. However, you never see SALES.PCTSUM used in the program and if you DID use SALES.PCTSUM in the program, you would get an error message in the log. Consider the log message that my program gets if I try to use a reference like SALES.PCTSUM for an alias in my program:

88   ods html file='c:\temp\test_compound.html' style=sasweb;
NOTE: Writing HTML Body file: c:\temp\test_compound.html
89   proc report data=sashelp.shoes nowd;
90     where region in ('United States', 'Canada');
91     column region product sales sales=salespct newvar1 newvar2;
92     define region / group;
93     define product / group;
94     define sales / sum 'Total Sales';
95     define salespct / pctsum 'Pctsum Sales' f=10.6;
96     define newvar1 / computed f=10.6;
97     define newvar2 / computed f=10.6;
98     compute newvar1;
99       newvar1 = salespct * 100;
100    endcomp;
101    compute newvar2;
102      newvar2=sales.pctsum * 100;
103    endcomp;
104  run;

ERROR: The variable type of SALES.PCTSUM is invalid in this context.
NOTE: The preceding messages refer to the COMPUTE block for newvar2.
NOTE: Will not run due to compilation errors.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 77 observations read from the data set SASHELP.SHOES.
      WHERE region in ('Canada', 'United States');
NOTE: PROCEDURE REPORT used (Total process time):
      real time           0.08 seconds
      cpu time            0.03 seconds

105  ods _all_ close;

  As you can see...SALES.PCTSUM is an invalid reference in the COMPUTE block. You MUST use the ALIAS name as the reference in the COMPUTE block. The reference to SALES.PCTSUM in the documentation topic you cited is only a conceptual reference. In order for the above program to work, the assignment statement for NEWVAR2 would have to be the same as the assignment statement for NEWVAR1 in order to eliminate the error message. In the documentation topic, Four Ways to Reference Report Items in a Compute Block,  (http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n0jann0dticnpbn1woe95...), you will find a table that shows exactly how and when you use a compound name versus a "regular" or simple name. Under the ANALYSIS usage, it says that you use a COMPOUND name except (note the * comment) that "If the variable has an alias, then you must reference it with the alias."

    I hope this explains the documentation example in a bit more detail.

cynthia

.

robertrao
Quartz | Level 8


Hi,

That was very informative and very detailed. I thank you for your time.

think I mistook the Diagrams in this notes from Documentation for a PDV when he is just showing how the row is filled!!!!

http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473631.htm

The example is from this same link

Thanks a ton

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
  • 2 replies
  • 1059 views
  • 0 likes
  • 2 in conversation