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

Hello,

I have a data/table like below:

stataq_0-1713753703871.png

If I would like to remove (XX) when rowlbl='All' for col1:col3, how can I do it using do loop?

I can do it one by one, but when I try to use do loop, I failed. Could anyone guide me on this?

 

*do it one by one;
data _ds;
set ds;
if rowlbl='All' then do;
col1=scan(col1, 1, ' (');
end;
run;

*try to use do loop;
data _ds;
set ds;
if rowlbl='All' then do;
do i=1 to 3;
coli=scan(coli, 1, ' (');
end;
end;
run;

My way of removing '(XX)' might be too silly. Any better suggestion?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What variable is COLI?

Did you mean to create an array?  if so you need addition syntax, brackets, to reference a member of the array.  

data _ds;
  set ds;
  array columns col: ;
  if rowlbl='All' then do index=1 to dim(columns);
    columns[index]=scan(columns[index], 1, ' (');
  end;
  drop index;
run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

What variable is COLI?

Did you mean to create an array?  if so you need addition syntax, brackets, to reference a member of the array.  

data _ds;
  set ds;
  array columns col: ;
  if rowlbl='All' then do index=1 to dim(columns);
    columns[index]=scan(columns[index], 1, ' (');
  end;
  drop index;
run;
stataq
Quartz | Level 8

😅 COL1 is just col1. I can update cols one by one. I only listed COL1. sorry for that.

 

I was wondering whether we could use do loop to update the variable values. If so, how?

Tom
Super User Tom
Super User

@stataq wrote:

😅 COL1 is just col1. I can update cols one by one. I only listed COL1. sorry for that.

 

I was wondering whether we could use do loop to update the variable values. If so, how?


??  Read the code I posted.

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
  • 3 replies
  • 543 views
  • 1 like
  • 2 in conversation