DATA Step, Macro, Functions and more

An array for trim function

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 106
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: 7,080

Re: An array for trim function

Posted in reply to sarahsasuser

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: 7,080

Re: An array for trim function

Posted in reply to sarahsasuser

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: 106

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: 11,343

Re: An array for trim function

Posted in reply to sarahsasuser

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: 7,080

Re: An array for trim function

Posted in reply to sarahsasuser

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,518

Re: An array for trim function

Posted in reply to sarahsasuser

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

Super User
Posts: 10,046

Re: An array for trim function

Posted in reply to Astounding

Bingo  Astounding  Smiley Happy

Frequent Contributor
Posts: 106

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 and locked.

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

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