- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data text;
length condition $200. variable $100. ;
condition="LBTESTCD = 'LDL' and VISIT = 'VISIT 1/SCREENING'";variable='LBTESTCD VISIT';output;
condition="NAME = 'JOHN' and SEX ='M' and TEXT = 'MY NAME is JOHN'";variable='NAME SEX TEXT';output;
condition="SPORT = 'FOOTBALL' and PLAYER = 'RONALDO' and text = 'Sport Champion'";variable='SPORT PLAYER TEXT';output;
run ;
data want ;
set text ;
do a= 1 to countw(variable) ;
var=strip(scan(variable,a,' '));
condition=tranwrd(condition,strip(var)||' ',cats('DSN','.',strip(var)||' '));
end ;
run;
In the above code, the condition variable having SAS logical conditions and variable is having only variable names extracted from the CONDITION. The task is to append the data set name with the variable. In first condition VISIT is both a variable and also a value embedded in quotes ,in second condition NAME is variable and also value same as SPORT in third condition. Since the above code is using TRANWRD funtion it is replacing the variable names whereever it is present, but the values with variable name should not be replaced like VISIT 1 is value it should not be appended with DSN since it is not variable. How to avoid this replacement from values.
OUTPUT:
DSN.LBTESTCD= 'LDL' and DSN.VISIT= 'DSN.VISIT1/SCREENING'
DSN.NAME= 'JOHN' and DSN.SEX='M' and DSN.TEXT= 'MY DSN.NAME is JOHN'
DSN.SPORT= 'FOOTBALL' and DSN.PLAYER= 'RONALDO' and text = 'Sport Champion'
EXPECTED OUTPUT:
DSN.LBTESTCD= 'LDL' and DSN.VISIT= 'VISIT1/SCREENING' ;
DSN.NAME= 'JOHN' and DSN.SEX='M' and DSN.TEXT= 'MY NAME is JOHN'
DSN.SPORT= 'FOOTBALL' and DSN.PLAYER= 'RONALDO' and text = 'Sport Champion' ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If I am following this properly (and I may not be), it seems like your code is creating the problem
condition=tranwrd(condition,strip(var)||' ',cats('DSN','.',strip(var)||' '));
If you take DSN out of this line of code, you get the desired results.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If i remove the DSN from the code i cannot append the variable name with respective DSN name as below.
DSN.LBTESTCD= 'LDL' and DSN.VISIT= 'VISIT1/SCREENING' .
As shown abvoe i need to append/concat the dataset name with Variable. If LBTESTCD is the variable then i have to display like DSN.LBTESTCD. If i remove DSN then there wont be any alias/dataset name to the variable. The issue here is if VISIT is the variable name and VISIT is also the value then DSN should be appended only to the Variable name . but not to the value embedded in quotes.
Hope i have cleared the question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@keen_sas wrote:
If i remove the DSN from the code i cannot append the variable name with respective DSN name as below.
DSN.LBTESTCD= 'LDL' and DSN.VISIT= 'VISIT1/SCREENING' .
You just need a more sophisticated rule, where DSN. is placed in some places but not other.
Paige Miller
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @keen_sas
I have added a statement using the PRXCHANGE function.
It looks for the following pattern:
- An equal sign (=) followed by :
- zero, one or more spaces (.*), a single quote ('), zero, one or more characters (= group 1 identified as $1) followed by
- "DSN." (= group 2) followed by
- one or more characters (.+) (= group 3 identified as $3)
If this pattern is found one or more times, group 2 is removed (<=> it keeps only = group1 group3)
data text;
length condition $200. variable $100.;
condition="LBTESTCD = 'LDL' and VISIT = 'VISIT 1/SCREENING'";
variable='LBTESTCD VISIT';
output;
condition="NAME = 'JOHN' and SEX ='M' and TEXT = 'MY NAME is JOHN'";
variable='NAME SEX TEXT';
output;
condition="SPORT = 'FOOTBALL' and PLAYER = 'RONALDO' and TEXT = 'Sport Champion'";
variable='SPORT PLAYER TEXT';
output;
run;
data want;
set text;
do a=1 to countw(variable);
var=strip(scan(variable, a, ' '));
condition=tranwrd(condition, strip(var)||' ', cats('DSN', '.',
strip(var)||' '));
end;
drop a;
condition=prxchange("s/=(\s*'.*)(DSN.)(.+')/=$1 $3/", -1, condition);
run;