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

Hi,

 

I have two data sets. The first one contains a list of patient with a description of their medical condition. The second one contains a list of code associated with key word.

 

I removed spaces between words and put everything in capital letter to make data matching easier.

 

The data set "subject" contains description of medical condition looks like this (only one observation is shown but I have 20,000) :

IDCONCLUSION
1SIGNOFARRHYTMIA

 

The data set "codes" contains codes associated with key word looks like this :

codeyes_ayes_bno_a
1.5.1ARRHYTMIAARHYTMIANOSIGN

 

 

I created a macro variable for every variable of the "codes" data set (see below)

 

data _null_; 
set codes;
	call symput("code",(code));
	call symput("yes_a",(yes_a));
	call symput("yes_b",(yes_b));
	call symput("no_a",(no_a));
run;

After that, I told SAS "Look in the "conclusion" variable and if you can find the a match between the conclusion and the macro variable '&yes_a' then do a = 1" (see below).

 

data subject_match; set subject;
if find (conclusion, '&yes_a') then a = 1;
if find (conclusion, '&no_a') then z = 0; 
run;

The problem is that this step doesn't work. I have no error code in the log but even if the words after in the string, "a" never equals to 1.

 

Can anyone help me with that?

 

Thank you in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
dera
Obsidian | Level 7

Thanks to each of you for your answers.

 

I figured the solution by myself. I only added the trim function before calling my macro variable.

 

data subject_match; set subject;
   a = find(conclusion, trim("&yes_a");
   z = find(conclusion, trim("&no_a");
run;

It worked perfectly as I wished.

 

 

Cheers

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

After this step

data _null_; 
set codes;
	call symput("code",(code));
	call symput("yes_a",(yes_a));
	call symput("yes_b",(yes_b));
	call symput("no_a",(no_a));
end;
run;

only the data from the last observation in codes will be stored in the 4 macro variables.

dera
Obsidian | Level 7
I know that. I will add a "do loop" after for all the observation in the codes data set. But for now, I only test it with a single code and it does not work.
When I do %PUT yes_a = &yes_a, the log shows me that it succesfully find it.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, first off macro variables need to be in double quotes to dereference:

if find (conclusion, "&yes_a") then a = 1;

Secondly, always put the dot after macro variables, it is best practice as sometimes it will matter. 

 

Third, coding is more complicated than just finding strings in other strings, there could be any number of connotations.

dera
Obsidian | Level 7
I add the double quotes and still do difference. Do you think that it may have to do with the fact that the FIND function already ask for double quote when you want to find a string? SAS might think that I am looking for the &yes_a word and not what is related to (i.e the word arrhytmia for example).
ballardw
Super User

@dera wrote:

Hi,

 

I have two data sets. The first one contains a list of patient with a description of their medical condition. The second one contains a list of code associated with key word.

 

I removed spaces between words and put everything in capital letter to make data matching easier.

 

The data set "subject" contains description of medical condition looks like this (only one observation is shown but I have 20,000) :

ID CONCLUSION
1 SIGNOFARRHYTMIA

 

The data set "codes" contains codes associated with key word looks like this :

code yes_a yes_b no_a
1.5.1 ARRHYTMIA ARHYTMIA NOSIGN

 

 

I created a macro variable for every variable of the "codes" data set (see below)

 

data _null_; 
set codes;
	call symput("code",(code));
	call symput("yes_a",(yes_a));
	call symput("yes_b",(yes_b));
	call symput("no_a",(no_a));
end;
run;

After that, I told SAS "Look in the "conclusion" variable and if you can find the a match between the conclusion and the macro variable '&yes_a' then do a = 1" (see below).

 

data subject_match; set subject;
if find (conclusion, '&yes_a') then a = 1;
if find (conclusion, '&no_a') then z = 0; 
run;

The problem is that this step doesn't work. I have no error code in the log but even if the words after in the string, "a" never equals to 1.

 

Can anyone help me with that?

 

Thank you in advance.

 


Putting a bunch of values into macro variables is quite often the symptom of a not very well thought out process and often a misunderstanding of the macro processor. For example in this case the macro variable only contain the values from the last record of data set codes if that step executed at all. The "end" has no do so that given data _null_ step won't execute at all.

 

Second when wanting to use the value of a macro variable as text you use double quotes "  such as "&yes_a." not the single quotes. Other wise the value would not resolve at all.

 

How many records are in your "codes" data set?

 

dera
Obsidian | Level 7

about 350 codes

dera
Obsidian | Level 7

Thanks to each of you for your answers.

 

I figured the solution by myself. I only added the trim function before calling my macro variable.

 

data subject_match; set subject;
   a = find(conclusion, trim("&yes_a");
   z = find(conclusion, trim("&no_a");
run;

It worked perfectly as I wished.

 

 

Cheers

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
  • 7 replies
  • 3739 views
  • 0 likes
  • 4 in conversation