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

I am trying to append dataset name (DSN) for each variable present in the logical condition, the variable names are present in another column LISTVAR and logical condition in COND

 

Sample code: 

 

data have;
domain="ABC";
listvar="ABCAT ABTESTCD ABTEST ABTXT";
cond="ABCAT = 'ADMINISTRATION' AND ABTESTCD = 'ASSIST' AND ABTEST = 'ASSISTANCE' AND ABTXT = 'EVENT'";
run;

 

data want;
set have;
do a= 1 to countw(listvar) ;
var=compress(scan(listvar,a,' '));
cond=tranwrd(cond,strip(var),cats(domain,'.',strip(var)));
end ;
run ;

Required output:DSN.ABCAT = 'ADMINISTRATION' AND DSN.ABTESTCD = 'ASSIST' AND DSN.ABTEST = 'ASSISTANCE' AND DSN.ABTXT = 'EVENT'


Current output: DSN.ABCAT = 'ADMINISTRATION' AND DSN.DSN.ABTESTCD = 'ASSIST' AND DSN.ABTEST = 'ASSISTANCE' AND DSN.ABTXT = 'EVENT'

 

Here the problem is both ABTESTCD and ABTEST are present in the variable list , initially ABTESTCD is appended with domain name i.e DSN.ABTESTCD , when it goes for ABTEST
the ABTESTCD is again appending with DSN so it is becoming DSN.DSN.ABTESTCD, here the dataset is getting appended twice because of ABTEST.
How to avoid such similar strings to get replaced by TRANWRD and the TRANWRD is also not functional conditionally also. Like i have tested by considering the length of the variable,but still replaces it . Is their any way to avoid this and replace only the exact word/string and this has to be dynamic since there will be many such scenarios.Can such scenarios be handled using PRXCHANGE?

Tried with arrays for each word creation, but i think its little lengthy.

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

Hi,

 

maybe like that:

 

data want;
  set have;
  length cond2 $ 256; /* assure enough space for a variable*/
  cond2 = cond;
  cond2 = tranwrd(cond, "=" , " = "); /* assure extra space between variable name and "=" */
  do a = 1 to countw(listvar) ; 
    var=compress(scan(listvar,a,' '));
    cond2=tranwrd(cond2
                 ,strip(var)!!" " /* serach for "WORD " instead "WORD"  */
                 ,cats(domain,'.',strip(var))!!" ");
  end ;
run ;

 

all the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

1 REPLY 1
yabwon
Amethyst | Level 16

Hi,

 

maybe like that:

 

data want;
  set have;
  length cond2 $ 256; /* assure enough space for a variable*/
  cond2 = cond;
  cond2 = tranwrd(cond, "=" , " = "); /* assure extra space between variable name and "=" */
  do a = 1 to countw(listvar) ; 
    var=compress(scan(listvar,a,' '));
    cond2=tranwrd(cond2
                 ,strip(var)!!" " /* serach for "WORD " instead "WORD"  */
                 ,cats(domain,'.',strip(var))!!" ");
  end ;
run ;

 

all the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1 reply
  • 1486 views
  • 0 likes
  • 2 in conversation