Contributor
Posts: 49

# Uninitialized Variable?

This is the tail end of another discussion but it's on a separate issue. I was under the impression if you define a variable upstream via calculation, SAS would recognize the variable downstream in subsequent calcs. In the case below, SAS is saying my variables A through L are uninitialized.

It's counter intuitive to me why variable "B" in the below code, would be uninitialized.

Set Actual_Points;

/*

-=Existing Point Variables=-

Points_Feb

Points_Mar

Points_Apr

Points_May

Points_Jun

Points_Jul

Points_Aug

Points_Sep

Points_Oct

Points_Nov

Points_Dec

Points_Jan;

*/

/* January  */

IF Points_Jan <0 THEN Jan2 = 0 AND B = SUM(Points_Jan,Points_Dec);

ELSE IF Points_Jan >= 0 THEN Jan2 = Points_Jan AND B = Points_Dec;

/* December  */

IF B <0 THEN Dec2 = 0 AND C = SUM(B,Points_Nov);

ELSE IF B >= 0 THEN Dec2 = Points_Dec AND C = Points_Nov;

/* November  */

IF C <0 THEN Nov2 = 0 AND D = SUM(C,Points_Oct);

ELSE IF C >= 0 THEN Nov2 = Points_Nov AND D = Points_Oct;

/* October  */

IF D <0 THEN Oct2 = 0 AND E = SUM(D,Points_Sep);

ELSE IF D >= 0 THEN Oct2 = Points_Oct AND E = Points_Sep;

/* September  */

IF E <0 THEN Sep2 = 0 AND F = SUM(E,Points_Aug);

ELSE IF E >= 0 THEN Sep2 = Points_Sep AND F = Points_Aug;

/* August  */

IF F <0 THEN Aug2 = 0 AND G = SUM(F,Points_Jul);

ELSE IF F >= 0 THEN Aug2 = Points_Aug AND G = Points_Jul;

/* July  */

IF G <0 THEN Jul2 = 0 AND H = SUM(G,Points_Jun);

ELSE IF G >= 0 THEN Jul2 = Points_Jul AND H = Points_Jun;

/* June  */

IF H <0 THEN Jun2 = 0 AND I = SUM(H,Points_May);

ELSE IF H >= 0 THEN Jun2 = Points_Jun AND I = Points_May;

/* May  */

IF I <0 THEN May2 = 0 AND J = SUM(I,Points_Apr);

ELSE IF I >= 0 THEN May2 = Points_May AND J = Points_Apr;

/* Apr  */

IF J <0 THEN Apr2 = 0 AND K = SUM(J,Points_Mar);

ELSE IF J >= 0 THEN Apr2 = Points_Apr AND K = Points_Mar;

/* Mar  */

IF K <0 THEN Mar2 = 0 AND L = SUM(K,Points_Feb);

ELSE IF K >= 0 THEN Mar2 = Points_Mar AND L = Points_Feb;

/* Feb  */

IF L <0 THEN Feb2 = 0;

ELSE IF L >= 0 THEN Feb2 = Points_Feb;

RUN;

Super User
Posts: 5,888

## Re: Uninitialized Variable?

What do you mean upstream?

B is not defined in the input data set (as defined in your comment section), nor defined in the data step itself.

Data never sleeps
Valued Guide
Posts: 864

## Re: Uninitialized Variable?

The way you have this set up is incorrect, I edited a small portion to help give you the right idea.  Take a look below, test with your data to make sure you are getting the correct output:

data want;

FORMAT date_time datetime22.3;

set have;

date_time = input(put(date_value,ddmmyy10.),anydtdtm.);

IF Points_Jan <0 then do;

Jan2 = 0;

B = SUM(Points_Jan,Points_Dec);

end;

ELSE IF Points_Jan >= 0 then do;

Jan2 = Points_Jan;

B = Points_Dec;

end;

run;

Super User
Posts: 9,599

## Re: Uninitialized Variable?

Hi,

Sorry, I think you need to look at the SAS guidance again as your code is wrong:

The dataset Actual_Points I am assuming has no variable B, yes.  So.

Set Actual_Points;

/* January  */

IF Points_Jan <0 THEN Jan2 = 0 AND B = SUM(Points_Jan,Points_Dec);

ELSE IF Points_Jan >= 0 THEN Jan2 = Points_Jan AND B = Points_Dec;

It is this misunderstanding of if/then constructs which is your problem.  What you should have is:

if <logical argument> then <operation>;

And for multiple operations:

if <logical argument> then do;

<operation>;

<operation>;

end;

In your example the b=sum() is trying to evaulate as part of the logical argument, where it is not defined.  Re-write it to:

if Points_Jan < 0 then do;

Jan2=Points_Jan;

B = SUM(Points_Jan,Points_Dec);

end;

else ...;

However this also leads onto other aspects of your code.  Why do you have so many if then constructs?  Try array processing.  If you can provide test data, in the form of a datastep, and required output you may get some code examples, but a brief syntax:

array months{12} points_jan-points_dec;

array results{12} 8.;

do i=1 to 12;

if months{i} < 0 then do;

Just to add on this, I generally don't recommend having data as column names, e.g. months.  This is ok if you only have a few, but what happens if you have 15 years, with 12 months each etc.  I would suggest either keeping them as a listable format - e,g, col1 col2 col3 etc. or normalising your data.

Super User
Posts: 6,785

## Re: Uninitialized Variable?

The basic reason you are having a problem is that you think this code assigns a value to B:

IF Points_Jan <0 THEN Jan2 = 0 AND B = SUM(Points_Jan,Points_Dec);

ELSE IF Points_Jan >= 0 THEN Jan2 = Points_Jan AND B = Points_Dec;

It doesn't.  It assigns a value to JAN2 only, not to B, as if you had parentheses in place:

IF Points_Jan <0 THEN Jan2 = ( 0 AND B = SUM(Points_Jan,Points_Dec) );

ELSE IF Points_Jan >= 0 THEN Jan2 = ( Points_Jan AND B = Points_Dec );

So later references to B give you the message about B being uninitialized.  Others have posted the proper syntax, using THEN DO and using separate assignment statements.

Good luck.

Discussion stats
• 4 replies
• 482 views
• 0 likes
• 5 in conversation