BookmarkSubscribeRSS Feed
keen_sas
Quartz | Level 8


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' ;

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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
keen_sas
Quartz | Level 8

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.

PaigeMiller
Diamond | Level 26

@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
ed_sas_member
Meteorite | Level 14

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 345 views
  • 0 likes
  • 3 in conversation