BookmarkSubscribeRSS Feed
PROCSAS
Calcite | Level 5

I am current using SAS 9.4. I am trying to an insert arrow images in Proc Report but the compute statement is not working

I am getting CHANGE_IN_PCT is uninitialized in the Log and if statement is not working, here CHANGE_IN_PCT is a numeric data type variable.

Please check the below code:

PROC REPORT DATA=test NOWD;

COLUMNS TYPE  PREVIOUS_YEAR_PCT CURRENT_YEAR_PCT CHANGE_IN_PCT   ;

DEFINE TYPE / "Type";

DEFINE PREVIOUS_YEAR_PCT / "Avg Previous Year %";

DEFINE CURRENT_YEAR_PCT / "Avg Current Year %";

DEFINE CHANGE_IN_PCT /  "Change in %" ;

compute CHANGE_IN_PCT  ;

if  CHANGE_IN_PCT > 0 then

call define (_col_,"style","Style=

[postimage= 'F:\EIAS\School Grades\Dashboard Data\GreenUp.gif']");

endcomp;

RUN;

7 REPLIES 7
stat_sas
Ammonite | Level 13

Hi,

Can we refer compute variable name in if statement?

Thanks,

compute CHANGE_IN_PCT ;

if  CHANGE_IN_PCT > 0 then

Cynthia_sas
SAS Super FREQ

Hi:

  Again, what is the usage of COMPUTE_IN_PCT in the DEFINE statement? Is the usage DISPLAY, ANALYSIS or COMPUTED -- does the variable exist in the input data set or are you creating it?

cynthia

stat_sas
Ammonite | Level 13

Thanks Cynthia - This is true define statement will determine usage of COMPUTE_IN_PCT. Error in log "CHANGE_IN_PCT is uninitialized in the Log and if statement is not working" indicates that this variable is not in the dataset. It looks like this is being created using compute statement.

Regards,

Naeem

Cynthia_sas
SAS Super FREQ

Hi:

  Not true. For example, SASHELP.CLASS has 5 variables: NAME, AGE, SEX, HEIGHT and WEIGHT. I can easily generate an "unitialized" message if I don't use HEIGHT the correct way in a COMPUTE block. Run the attached code and you will see the message that HEIGHT is unitialized.

cynthia

ods _all_ close;
ods html file='c:\temp\geterror.html';

proc report data=sashelp.class nowd;
   column name age sex height weight;
   define name / order 'Name';
   define height / 'Height';
   compute height;
     if height gt 55 then
        call define(_col_,'style','{postimage="c:\temp\kermit.gif"}');
   endcomp;
run;
ods html close;

stat_sas
Ammonite | Level 13

Hi,

Height variable is coming from the dataset and is on the column statement. When we are using this in a compute block, actually we are trying to use this as a temporary variable but it has already been defined as an analysis variable, this generates the error message.

Regards,

Naeem

Cynthia_sas
SAS Super FREQ

Hi:

  What is actually happening is that HEIGHT is being treated as an ANALYSIS variable with a default statistic of SUM. In the COMPUTE block, you are NOT trying to use HEIGHT as a 'temporary" variable, it is being referred to incorrectly in the COMPUTE block. In a COMPUTE block any reference to an existing numeric variable MUST use a COMPOUND name of variable.statistic, as explained here, in the doc:

Base SAS(R) 9.3 Procedures Guide, Second Edition Look at the section entitled:

"Four Ways to Reference Report Items in a Compute Block". You can use temporary items in a COMPUTE block, but in the code below, HEIGHT is a report item coming from the dataset and HEIGHT.SUM is the compound name that must be used in the COMPUTE block to change the style.

  So, for example, if you try this alternate code, you will see that the error goes away. I changed the CALL DEFINE to use a simple style override, since not everybody has kermit.gif on their system. In the code below the use of the simple reference to HEIGHT will get a note in the log for #2 and #3, the correct reference to HEIGHT.SUM (not a temporary item) will not generate a message.

cynthia

ods _all_ close;
ods html file='c:\temp\geterror.html';

proc report data=sashelp.class nowd;
  title1 '1) Will Get Error';
   column name age sex height weight;
   define name / order 'Name';
   define height / 'Height';
   compute height;
     if height gt 55 then
        call define(_col_,'style','style={background=lightgreen}');
   endcomp;
run;
     
proc report data=sashelp.class nowd;
  title1 '2) Will NOT Get Error';
   column name age sex height weight;
   define name / order 'Name';
   define height / 'Height';
   compute height;
     if height.sum gt 55 then
        call define(_col_,'style','style={background=lightgreen}');
   endcomp;
run;
  
proc report data=sashelp.class nowd;
  title1 '3) Will NOT Get Error';
   column name age sex height weight;
   define name / order 'Name';
   define height / 'Height';
   compute height;
     if height.sum gt 55 then
        call define(_col_,'style','style={postimage="c:\temp\arrow.gif"}');
   endcomp;
run;

         
ods html close;


height_uninit.pngArrow.gif
Cynthia_sas
SAS Super FREQ

Hi:

  Is your variable character or numeric. I suspect that it must be numeric, because if it is, you would need to refer to it by the compound name that is expected in the COMPUTE block. The PROC REPORT doc is very clear on when you need to use a compound name and without a specific usage in your DEFINE statement, PROC REPORT will assign the default usage for the type.

cynthia

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 1953 views
  • 2 likes
  • 3 in conversation