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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

7 REPLIES 7
Tom
Super User Tom
Super User

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;

sarahsasuser
Quartz | Level 8

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;

ballardw
Super User

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

Tom
Super User Tom
Super User

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;

Astounding
PROC Star

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

Ksharp
Super User

Bingo  Astounding  Smiley Happy

sarahsasuser
Quartz | Level 8

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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