BookmarkSubscribeRSS Feed
benjaminchua002
Calcite | Level 5

I need year{i} to equal yearsum{i}. For now, I have it coded as below. It works but it's incredibly inefficient. Is there a shorter way to do this? I have two arrays: year1-year10 and yearsum1-yearsum10. Yearsum is the sum of values in each year (1 to 10).

if last then do;

Variable = 'Total';

year1 = yearsum1; year2 = yearsum2; year3 = yearsum3; year4 = yearsum4; year5 = yearsum5; year6 = yearsum6; year7 = yearsum7; year8 = yearsum8; year9 = yearsum9; year10 = yearsum10;
output;
end;

I can't use a DO i = 1 to 10 as 'Total' will show up 10 times. I only want one row of 'Total'. 

Capture.PNG

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

Using an array would be even less efficient, and would make the code harder to read.

Using a rename statement would be the most efficient.

PaigeMiller
Diamond | Level 26

@benjaminchua002 wrote:

I need year{i} to equal yearsum{i}. For now, I have it coded as below. It works but it's incredibly inefficient. Is there a shorter way to do this? I have two arrays: year1-year10 and yearsum1-yearsum10. Yearsum is the sum of values in each year (1 to 10).

if last then do;

Variable = 'Total';

year1 = yearsum1; year2 = yearsum2; year3 = yearsum3; year4 = yearsum4; year5 = yearsum5; year6 = yearsum6; year7 = yearsum7; year8 = yearsum8; year9 = yearsum9; year10 = yearsum10;
output;
end;

I can't use a DO i = 1 to 10 as 'Total' will show up 10 times. I only want one row of 'Total'. 

Capture.PNG


Restructuring your data in this fashion, to create a wide data set, is rarely needed, and there are easier ways to get the final result with 10 years going across and variable going down the left side with a total row below. PROC REPORT will get you there without the need to code each year separately or rename variables.

--
Paige Miller
Quentin
Super User

What do you mean by incredibly inefficient?  

 

If you would like to use your arrays so that your program is shorter, you can.  This decreases the amount of code to write, but would not decrease the execution time, because you're still doing 11 assignment statements.

 

Untested:

if last then do;
  Variable= "Total" ;
  do i=1 to dim(year) ;
    year{i}=yearsum{i} ;
  end ;
  output ;
end;

As of others have mentioned, this type of wide dataset often creates the need for clunky code like this.  Typically code is easier when data are structured vertically.  If you had a variable for Year, and variables for Wages Retirement Medical, then you would create a Total variable as just Total=sum(Wages,Retirement,Medical).

 

I feel like I spent the first 5 years of my SAS programming learning how to use arrays, and then spent the second 5 years learning to recognize when my thought to use an array was an indication of a data structure problem.

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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