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

Hi,

a while ago I created an array for finding daily dose of a medication in my project. My array related code was:

array day_array(1326) dose_day1-dose_day1326 ;
          do i = 1 to 1326 ;

 

Now a while later and in a different module of my project I have realized that I need to add up the daily doses of medication to find a total medication for each participant. I used:

Total_med=sum (of dose_day1-dose_day1326);

 

I am getting "Variable dose_day[i] is uninitialized" error (where i=1 to 1326)! Can anyone please help me with adding these up?

 

Thanks, 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

I am not allowed to download your file. Run a proc contents instead to show the structure.

You seem not to have the expected variables there.

These 2 solutions work:

data HAVE; retain VAR1-VAR998 ; run;        
data WANT; set HAVE; A=sum(of VAR1-VAR998); run;
data WANT; set HAVE; A=sum(of VAR:); run;

 

 

View solution in original post

6 REPLIES 6
ChrisNZ
Tourmaline | Level 20

From what you've explained, there is no reason for this message to be generated.

Please show use the full code and log.

Use the appropriate icons for pasting that.

Primavera
Quartz | Level 8

Thanks for your reply. I do not use this forum frequently and I am not good with icons and such (not very good with SAS either). I am very sorry. 

The code that I wrote was very simple.

data want;
set have;
Total_dose= sum (of dose_day1-dose_day1326);
run;

I have attached a shortened version of my have file. I just want to add up all the dose_day[i] in each row and store it in total_dose variable.

ChrisNZ
Tourmaline | Level 20

I am not allowed to download your file. Run a proc contents instead to show the structure.

You seem not to have the expected variables there.

These 2 solutions work:

data HAVE; retain VAR1-VAR998 ; run;        
data WANT; set HAVE; A=sum(of VAR1-VAR998); run;
data WANT; set HAVE; A=sum(of VAR:); run;

 

 

Primavera
Quartz | Level 8

Hi everyone,

Thank you very much for your helps. I closed SAS, restarted my computer, and reopened SAS and this time it worked!  

I used this very simple code as you suggested. I don't know what went wrong the first time but very glad it finally worked.

data want;                                                                                                                                              
set have;                                                                                                                                                                                                                                                                                                                       
Total_med= sum (of dose_day1-dose_day1326);                                                                                                                                 
run; 
mkeintz
PROC Star

There is nothing obviously wrong with your code.  But we can't help without more information.  Please provide the sas log in which you get the error messages.

 

If you're getting an error message for every variable, you can shorten the list of errors by using

Total_dose= sum (of dose_day1-dose_day13);

instead of

Total_dose= sum (of dose_day1-dose_day1326);

But, I would suggest also following @ChrisNZ 's recommendation to run a proc contents of the HAVE dataset - just to confirm that it actually has variables DOSE_DAY1-DOSE_DAY1326.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

@Primavera wrote:

Hi,

a while ago I created an array for finding daily dose of a medication in my project. My array related code was:

array day_array(1326) dose_day1-dose_day1326 ;
          do i = 1 to 1326 ;

 

Now a while later and in a different module of my project I have realized that I need to add up the daily doses of medication to find a total medication for each participant. I used:

Total_med=sum (of dose_day1-dose_day1326);

 

I am getting "Variable dose_day[i] is uninitialized" error (where i=1 to 1326)! Can anyone please help me with adding these up?

 

Thanks, 

 

 


I am going to assume that you heavily edited exactly what was in the log. Rerun the code, copy the log for the data step with the message and paste into a text box opened on the forum with the </> icon.

 

But I bet the issue is you do not have ALL the variables you told SAS to use in the sum statement. See this for an example.

 

22   data junk;
23      input x1 x2 x4;
24   datalines;

NOTE: The data set WORK.JUNK has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


26   ;
27
28   data want;
29      set junk;
30      s = sum(of x1-x4);
31   run;

NOTE: Variable x3 is uninitialized.
NOTE: There were 1 observations read from the data set WORK.JUNK.
NOTE: The data set WORK.WANT has 1 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

my data set has ONE record with values of 1 2 and 3 for the three variables in the data set.

The SUM statement is using four variables.Since X3 does not exist in the data set then there is message about it is uninitialized. There will be one note for each variable that does not exist. When you use a variable list like X1 - Xn then SAS expects ALL of X1, X2, X3, ..., Xn to exist.

So, either fix the data to have all of the "dose_day" values.

Or Perhaps use the colon list operator:   sum( of Dose_day: );

which will use all variables whose names start with "dose_day". There cannot be any gap between the part of the variable name and the colon.

 

Generally you may find in the long run that having 1000's of variables on a record is not the best way to use most data.

If each of those is supposed the "dose" given on a date you might have found that have data with ID values, the actual Date, and a single Dose variable would work. The proc means/summary could add up the values per group of Id values.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 1693 views
  • 0 likes
  • 4 in conversation