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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 2166 views
  • 3 likes
  • 5 in conversation