Hi.
In her excellent book (The SAS(R) Programmer's PROC REPORT Handbook: Basic to Advanced Reporting Techniques), Jane Eslinger gave an example of a computed character variable with PROC REPORT.
Can somebody explain why this computed variable did not work like its equivalent whith a DATA STEP and a PROC PRINT.
The code:
* Source pgm: Eslinger_TW19482_Program2-3.sas ;
title1 "Pgm2-3";
title2 "the computed variable demog is not ok";
proc report data=orders(obs=5);
column customer_name demog customer_age customer_gender customer_country customer_type;
define customer_age / display ;
define customer_gender / display ;
define customer_country / display ;
define demog / computed;
compute demog / char length=20;
demog = cats(customer_age, "/", customer_gender, "/", customer_country);
endcomp;
run;
title2 "the computed variable demog is ok";
data ordersmod;
length demog $ 20;
set orders(obs=5);
demog = cats(customer_age, "/", customer_gender, "/", customer_country);
run;
proc print data=ordersmod;
var demog;
var customer_name customer_age customer_gender customer_country;
run;
SAS version 9.4 TS1M3 used with Enterprise Guide 7.13
Source pgm and data can be found at: https://support.sas.com/downloads/package.htm?pid=1896
Thanks
Alain
SAS User
The problem is that the variables used in calculating DEMOG are defined after DEMOG itself. Therefore, they are missing at the time you try to compute DEMOG.
To get the computed variable right, you can either change the order of the variables, or you can define the variables used for computing twice (first time with an alias and the NOPRINT option), e.g.:
proc report data=orders(obs=5);
column customer_name customer_age=x1 customer_gender=x2 customer_country=x3
demog customer_age customer_gender customer_country;
define demog / computed;
define x1 / display noprint;
define x2 / display noprint;
define x3 / display noprint;
define customer_age / display ;
define customer_gender / display ;
define customer_country / display;
compute demog / char length=20;
demog = cats(x1, "/", x2, "/", x3);
endcomp;
run;
I'm mot familiar with SAS(R) language. Posting the error message could give a hint to the reason.
You may try use the call format instead the function one:
compute demog / char length=20;
call cats(demog, customer_age, "/", customer_gender, "/", customer_country);
endcomp;
The problem is that the variables used in calculating DEMOG are defined after DEMOG itself. Therefore, they are missing at the time you try to compute DEMOG.
To get the computed variable right, you can either change the order of the variables, or you can define the variables used for computing twice (first time with an alias and the NOPRINT option), e.g.:
proc report data=orders(obs=5);
column customer_name customer_age=x1 customer_gender=x2 customer_country=x3
demog customer_age customer_gender customer_country;
define demog / computed;
define x1 / display noprint;
define x2 / display noprint;
define x3 / display noprint;
define customer_age / display ;
define customer_gender / display ;
define customer_country / display;
compute demog / char length=20;
demog = cats(x1, "/", x2, "/", x3);
endcomp;
run;
Hi:
Exactly right answer. This is known as the "left-to-right" rule of PROC REPORT. The DATA step has a Program Data Vector, so the values of all the variables on each row are available to you while the DATA step is processing that row.
However, PROC REPORT only can see the variables in the COLUMN statement and there is NOT any Program Data Vector or buffer area in PROC REPORT that holds all the variables for a row. PROC REPORT places values from the data on the report row one value at a time, working from left to right. So let's say you have 5 variables on the report row, listed in the COLUMN statement:
column v1 v2 v3 v4 v5;
At the point in time when PROC REPORT is placing V2 on the report row, it has visibility of the values for V1 and V2, but does NOT know what the values for V3, V4 or V5 are. So you could not use any of these values in a COMPUTE block for V1 or V2 because they have not yet been placed on the report row.
That is why PROC REPORT does not work the same as the DATA step.
Cynthia
Hi Cynthia.
Thanks for the explanation about the differences between the Data Step PDV and the COLUMN "processing" in PROC REPORT.
Thanks to s_lassen too that brings 2 solutions to this problem.
Thanks to others by proposing ideas even if they were not solutions to this problem
Great Holidays to everybody!
AVO339 (Alain V. French Canadian SAS User)
And finally the exact SAS documentation reference is:
Base SAS® 9.4 Procedures Guide, Seventh Edition
page 1889
Note: The position of a computed variable is important. PROC REPORT assigns
values to the columns in a row of a report from left to right. Consequently, you
cannot base the calculation of a computed variable on any variable that appears
to its right in the report.
Bottom line: a lesson for me: reading the documentation is what to do first... 😉
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.