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

Hi there,

I have a SAS dataset called sasdata1 which has 40 variables with 20 of them named "attrib1" to "attrib20". There is also a variable called numattrib that stores the number of non-empty attributes starting from attrib1 (Obviously the value of numattrib could be different for different records). I want to create a sub-dataset called sasdata2 with records meeting the condition that any of attrib1-attrib20 starts with either "123" or "13a" or "2a1". Here is my SAS code:

 

DATA sasdata2;
SET sasdata1;
if

Substr(attrib1,1,3) in ('123','13a','2a1') or
Substr(attrib2,1,3) in ('123','13a','2a1') or

....

Substr(attrib20,1,3) in ('123','13a','2a1') or
;
RUN;

 

My first question is whether it is possible to consolidate these 20 lines of code after the key word if. Second question is whether we can take advantage of the variable numattrib to design a more efficient import process.

Thank you very much in advance. 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @sasfyj and welcome to the SAS Support Communities!

 

To utilize numattrib use an iterative DO loop "... TO numattrib."

 

Example:

data sasdata2(drop=i);
set sasdata1;
array a[*] attrib1-attrib20;
do i=1 to numattrib until(a[i] in: ('123','13a','2a1'));
end;
if i<=numattrib;
run;

View solution in original post

10 REPLIES 10
blueskyxyz
Lapis Lazuli | Level 10
please upload a sample dataset for the test
ChrisNZ
Tourmaline | Level 20

Would this work for you?

if index('|'||catx('|',of ATTRIB1-ATTRIB20), "|123") 
 | index('|'||catx('|',of ATTRIB1-ATTRIB20), "|13a") 
 | index('|'||catx('|',of ATTRIB1-ATTRIB20), "|2a1") then output;

 

ChrisNZ
Tourmaline | Level 20

Another way:

if prxmatch('/(~123|~13a|~2a1)/', '~'||catx('~',of ATTRIB1-ATTRIB20) ) then output;

 

Oligolas
Barite | Level 11

@Chris you need a starting anchor in the regex so IMHO catx is not suitable here

 

maybe something like this:

ARRAY attribs ATTRIB1-ATTRIB20;
DO i=1 to dim(ATTRIBS);
if not prxmatch('/^(123|13a|2a1)/',ATTRIBS(i)) then leave;
if i eq 20 then output;
END;
drop i;

 

________________________

- Cheers -

ChrisNZ
Tourmaline | Level 20

>  you need a starting anchor

You mean the tilde I add before catx() ?

Oligolas
Barite | Level 11

@ChrisNZ your solution is correct, my bad.

I meant that all the variables had to meet the prefix defined, but one is enough according to the requirements.

This is definitely haute couture of programming as I like it.

________________________

- Cheers -

sasfyj
Calcite | Level 5

Thanks for your help. I think we should remove 'Not' and change eq to le

sasfyj
Calcite | Level 5

Thank you. It works properly.

sasfyj
Calcite | Level 5

Thanks. It works as well. 

FreelanceReinh
Jade | Level 19

Hi @sasfyj and welcome to the SAS Support Communities!

 

To utilize numattrib use an iterative DO loop "... TO numattrib."

 

Example:

data sasdata2(drop=i);
set sasdata1;
array a[*] attrib1-attrib20;
do i=1 to numattrib until(a[i] in: ('123','13a','2a1'));
end;
if i<=numattrib;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 1059 views
  • 2 likes
  • 5 in conversation