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.
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
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
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!
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.
Ready to level-up your skills? Choose your own adventure.