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

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
Obsidian | Level 7

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

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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