DATA Step, Macro, Functions and more

An array for trim function

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 104
Accepted Solution

An array for trim function

Hi All,

I need to take a '.' out of 44 variables: var_1-var_44 and would like to do this with an array. The following code is not working. Can you help me troubleshoot?

Data...

     length vrn $8.;

     array varnew (*) var_1-var_44;

             do i=1 to dim(varnew);

            vrn=compress(trim(vrn), '.');

        end;

        drop i;

....

run;

Thanks.


Accepted Solutions
Solution
‎05-20-2015 01:21 PM
Super User
Super User
Posts: 6,494

Re: An array for trim function

You need to use VARNEW(I) to reference the current variable instead of VM.

Personally I would use DO OVER, but SAS is trying to remove that functionality.

array varnew var_1-var_44 ;

do over varnew;

varnew=compress(varnew,'.');

end;

View solution in original post


All Replies
Solution
‎05-20-2015 01:21 PM
Super User
Super User
Posts: 6,494

Re: An array for trim function

You need to use VARNEW(I) to reference the current variable instead of VM.

Personally I would use DO OVER, but SAS is trying to remove that functionality.

array varnew var_1-var_44 ;

do over varnew;

varnew=compress(varnew,'.');

end;

Frequent Contributor
Posts: 104

Re: An array for trim function

Thanks Tom that worked. However, if SAS is getting rid of DO OVER, what other options are there? When I reference varnew  in the compress function without the DO OVER, I get an error that the varnew var can't be referenced here. Example:

array varnew (*) var_1-var_44;

             do i=1 to dim(varnew);

            vrn=compress(trim(varnew), '.');

        end;

        drop i;

Super User
Posts: 10,454

Re: An array for trim function

If using the explicit array step, the i= 1 to dim() then you have to use the Explicit array reference varnew.

Super User
Super User
Posts: 6,494

Re: An array for trim function

If you want to use explicit array references then you do have to actually be explicit in your references, so make sure the include the index value when you use the array name in a statement.

Plus you need to write the result back to the same variable if you want it to have any effect.

Note that you really don't need the (*) as SAS knows that you have listed the variables and can count them itself.

And the TRIM() function is doing nothing in this context.

array varnew var_1-var_44;

do i=1 to dim(varnew);

  varnew(i)=compress(varnew(i), '.');

end;

drop i;

Super User
Posts: 5,069

Re: An array for trim function

Just on a hunch ... is it possible that these variables are numeric rather than character?

Super User
Posts: 9,656

Re: An array for trim function

Bingo  Astounding  Smiley Happy

Frequent Contributor
Posts: 104

Re: An array for trim function

Perhaps and could explain why character vs. numeric would make a difference in this case and what to do in the latter case? This is a character variable that I'm using in my example, but I'd be interested to know what I would need to change if I have a numeric variable in the future.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 434 views
  • 3 likes
  • 5 in conversation