DATA Step, Macro, Functions and more

Passing a Period through an array

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

Passing a Period through an array

[ Edited ]
proc contents data = SASHELP.ELECTRIC order=varnum; run;

data Have;
	set SASHELP.ELECTRIC(keep = Revenue Coal Hydro);
		array var Revenue   Coal    Hydro;
		array fmt DOLLAR10  COMMA10 COMMA10;
		array new t_Revenue t_coal  t_Hydro;
		
		do over var;
			new=put(var,fmt);
		end;
run;

How do I pass a comma through to my fmt array?

 

OR 

 

Is there an easier way to grab the formatted value lables and put into a new variable??

 

Thanks!!


Accepted Solutions
Solution
‎11-06-2016 08:40 AM
Super User
Posts: 10,500

Re: Passing a Period through an array

[ Edited ]

The functions PUTN and PUTC allow you to use the value of a text variable as the format.

Also the Do Over array statement isn't reliable when addressing multiple arrays.

A temporary array is a better way to use a list of values. The way you declared the FMT array it was expecting variables named Dollar10 and Comma10 not the values. Note that the values for the format include the . in the text vale.

I think you may be looking for something like this:

data Have;
	set SASHELP.ELECTRIC(keep = Revenue Coal Hydro);
		array var Revenue   Coal    Hydro;
		array fmt {3} $10 _temporary_ ( "DOLLAR10.",  "COMMA10." ,"COMMA10." );
		array new t_Revenue t_coal  t_Hydro;
		
		do i = 1 to dim(var);
			new[i]=putn(var[i],fmt[i]);
		end;
      drop i;
run;

 

View solution in original post


All Replies
Solution
‎11-06-2016 08:40 AM
Super User
Posts: 10,500

Re: Passing a Period through an array

[ Edited ]

The functions PUTN and PUTC allow you to use the value of a text variable as the format.

Also the Do Over array statement isn't reliable when addressing multiple arrays.

A temporary array is a better way to use a list of values. The way you declared the FMT array it was expecting variables named Dollar10 and Comma10 not the values. Note that the values for the format include the . in the text vale.

I think you may be looking for something like this:

data Have;
	set SASHELP.ELECTRIC(keep = Revenue Coal Hydro);
		array var Revenue   Coal    Hydro;
		array fmt {3} $10 _temporary_ ( "DOLLAR10.",  "COMMA10." ,"COMMA10." );
		array new t_Revenue t_coal  t_Hydro;
		
		do i = 1 to dim(var);
			new[i]=putn(var[i],fmt[i]);
		end;
      drop i;
run;

 

Contributor
Posts: 62

Re: Passing a Period through an array

can you tell me more about the dangers of multiple arrays in a do-over loop?

 

Thank you!

Super User
Posts: 10,500

Re: Passing a Period through an array

The first issue is it becomes very easy to generate an error of array out of bounds if all arrays are not of the same size.

data junk;
   input x1 - x5  y1-y4;
   array x x:;
   array y y:;
   do over x;
     put x=;
     put y=;
   end;;
datalines;
 1 2 3 4 5 6 7 8 9
 2 3 4 5 6 7 8 9 10
 ;
 run;

ANY use of the array y is going to have issues when you get to the fifth element of X in the above. And if you change it to use Y as the controller then the last element of X never gets processed. Yes the example is trivial but it demonstrates the principal.

 

Second is that when you need to conditionally reference an element. When using Do i = 1 to dim(x) you have the ability to check if the element you are processing in x is the such the first with "If i=1 then do"  or last: "if  i= dim(x) ".

Also any time when you may need to reference different elements of two arrays or mulitiple elements of the same array at the same time which may occur with ordering the elements of an array or finding the max,min, mean etc of groups of 3 or 4 elements is just not feasible.

 

Note that the DO OVER code is not even documented in SAS since version 8.

I would pretty much restrict the use of DO OVER to single operations on single arrays.

 

I shudder trying to use Do Over with a two dimensional array.

 

Contributor
Posts: 62

Re: Passing a Period through an array

Thanks! That example makes the dangers clear.

 

I'll be using explicit array references from now on Smiley Wink

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 262 views
  • 2 likes
  • 2 in conversation