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

Hello experts,

I have a sample dataset with 10 columns, all of them are text characters, and each column has over 400 rows.   I would like to search any rows that are over 50 characters in those 10 columns.   Please advise how to approach it.  Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

If you've decided on 10 rows.

it's a simple code.

 

proc sql;
  create table want as 
  select * from sample
  where length(col1)>50 or
        length(col2)>50 or
        length(col3)>50 or
        length(col4)>50 or
        length(col5)>50 or
        length(col6)>50 or
        length(col7)>50 or
        length(col8)>50 or
        length(col9)>50 or
        length(col10)>50
  ;
quit;

If the number of columns fluctuates, you can use the max and length functions to find the maximum number of characters and check if it exceeds 50.

data want;
  set sample;
  array c{*} col1-col10;
  do i=1 to dim(c);
    maxl=max(maxl,length(c{i}));
  end;
  if maxl>50;
  drop i;
run;

View solution in original post

2 REPLIES 2
Reeza
Super User

And do what with them? What do you want as output?

 


@ybz12003 wrote:

Hello experts,

I have a sample dataset with 10 columns, all of them are text characters, and each column has over 400 rows.   I would like to search any rows that are over 50 characters in those 10 columns.   Please advise how to approach it.  Thank you.


data want;
set have;

array _chars(10) list of variables here;
array _l(10) len1-len10;

do i=1 to 10;
if length(_chars(i)) >= 50 then _l(i) =1;
else _l(i)=0;
end;

run;
japelin
Rhodochrosite | Level 12

If you've decided on 10 rows.

it's a simple code.

 

proc sql;
  create table want as 
  select * from sample
  where length(col1)>50 or
        length(col2)>50 or
        length(col3)>50 or
        length(col4)>50 or
        length(col5)>50 or
        length(col6)>50 or
        length(col7)>50 or
        length(col8)>50 or
        length(col9)>50 or
        length(col10)>50
  ;
quit;

If the number of columns fluctuates, you can use the max and length functions to find the maximum number of characters and check if it exceeds 50.

data want;
  set sample;
  array c{*} col1-col10;
  do i=1 to dim(c);
    maxl=max(maxl,length(c{i}));
  end;
  if maxl>50;
  drop i;
run;
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
  • 2 replies
  • 1107 views
  • 5 likes
  • 3 in conversation