BookmarkSubscribeRSS Feed
JackZ295
Pyrite | Level 9

Hi! In my data step: 

 

/*Setting Length for redcap_event_name in Databse and Inputting QI Variable Values*/
%let RR=rr100d;
%let RT=rt190; 
data three; 
length id $ 6; 
length record_id $ 30;
length redcap_event_name $ 30; 
set two; 

qi104="Date of scan acquisition on the RT Form (Item 90: &RT) is after the date the radiology review is completed on the RR Form (Item d: &RR).
The date of scan acquisition should be before the date of radiology review. Please review forms for the accuracy of the dates and or return this query with your comments";
run; 

I am trying to create a variable, qi104, that pulls in the value of the variables rt190 and rr100d in the quote for each of the observations. Each observation has unique values for rt190 and rr100d. However, it seems that my macro syntax just pulls in the text "rt190" or "rr100d" rather than the value of those respective variables for the quoted string represented by variable qi104. What am I doing wrong? Any input regarding this would be much appreciated. Thanks so much! 

10 REPLIES 10
yabwon
Onyx | Level 15

No need for macrovariables here:

 

data three; 
length id $ 6; 
length record_id $ 30;
length redcap_event_name $ 30; 
set two; 

qi104=cats(
"Date of scan acquisition on the RT Form (Item 90: "
,rr100d
,") is after the date the radiology review is completed on the RR Form (Item d: "
,rt190
,"). The date of scan acquisition should be before the date of radiology review. Please review forms for the accuracy of the dates and or return this query with your comments"
);
run; 

 

 

A word of explanation.

Your original code won't do what you expect because macrovariables values were "rr100d" and "rt190" and those values were inserted into the text string. Macrovariables work as a "code text" replacement and a text like: "blablabal... &RR ...blablabla" is transformed by macroprocessor to: "blablabal... rr100d ...blablabla".

 

Bart 

 

[EDIT:]

Read the documetation: 

1) https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p1jgr545j3yz29n1uic9qlvlrzo4.htm

2) https://documentation.sas.com/doc/en/pgmsascdc/v_049/sqlproc/p0lsf4btafkw9mn1md4c69kkfwbl.htm

or watch the tutorial:

3) https://www.youtube.com/watch?v=ODq6V69IX2Y

 

_______________
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



JackZ295
Pyrite | Level 9

Thanks for your help @yabwon ! Does the cats function not seem to agree with particular characters (e.g. parentheses or colons)? For some reason, when I try to run a similar code, qi104 ends up being blank, even though there is no error code saying that it's uninitiated. 

PaigeMiller
Diamond | Level 26

What "similar code"? Show us the code. Show us the unexpected output. Show us a portion of the data for q104.

--
Paige Miller
yabwon
Onyx | Level 15

@PaigeMiller already wrote what you should do to get our help...

 

Bart

 

P.S. the CATS() works like this:

result = cats(string1, string2, ..., stringN);
_______________
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



JackZ295
Pyrite | Level 9

Hi @yabwon , @ballardw , and @PaigeMiller ; thanks for your help. @yabwon , your syntax was the one that I did use. My code is as follows: 

 

data three; 
length id $ 6; 
length record_id $ 30;
length redcap_event_name $ 30; 
set two; 
where rt190 > rr100d and rt190 ne . and rr100d ne .; 
rtdate=put(rt190, MMDDYY10.); 
rrdate=put(rr100d, MMDDYY10.); 
number=_n_;
id=put(number, z6.);  
event_id=48807; 
record_id_complete=2; 

if  rr100d <> . then do;
form_letter_1="RR";
page="rr_ct_scan_acquisition_form"; 
end;

if rt190 <> . then do; 
form_letter_2="RT"; 
page="rt_ct_scan_acquisition_form"; 
end; 

qi100a=upcase(redcap_data_access_group); 
qi100c="rt_after_rr";
qi100b=cats(qi100c, '_', id);
record_id=qi100b;
qi100d='02APR2024'd;
qi100e="V01";
qi100q="AXA010";
qi100r=1;
qi101=1;
qi101_s1a=pi100b;
qi101_s1b=pi100c;
qi102___a=1;
qi103___b=1;
qi104=cats('Date of scan acquisition on the RT Form (Item 90: ', rtdate, ')', 
'is after the date the radiology review is completed on the RR Form (Item d:', rrdate, '). ', 'The date of scan acquisition should be before the date of radiology review. Please review forms for the accuracy of the dates and or return this query with your comments.'); run; proc print data=three; var qi104; run;

The relevant log is as follows: 

 

WARNING: In a call to the CATS function, the buffer allocated for the result was not long enough to
         contain the concatenation of all the arguments. The correct result would contain 317
         characters, but the actual result might either be truncated to 200 character(s) or be
         completely blank, depending on the calling environment. The following note indicates the
         left-most argument that caused truncation.
NOTE: Argument 7 to function CATS('Date of scan'[12 of 50 characters shown],'04/22/2023',')','is
      after the'[12 of 75 characters shown],'04/22/1996','). ','The date of '[12 of 170 characters
      shown]) at line 2877 column 7 is invalid.

Any thoughts?

 

 

Tom
Super User Tom
Super User

If you don't define you variables, like qi104, then SAS is forced to GUESS how you intended them to be defined.

 

Looks it guessed to make it too short for the strings you are trying to store in it.

 

You seem to know how to define variables since you are doing it for id, record_id and redcap_event_name.

 

JackZ295
Pyrite | Level 9

Hi @Tom , many thanks for your help! It seems to be a length issue for the concatenated variable. Would you recommend that I pre-define the length of the variable where I am trying to concatenate various strings of text? Thanks again for your help! 

Tom
Super User Tom
Super User

@JackZ295 wrote:

Hi @Tom , many thanks for your help! It seems to be a length issue for the concatenated variable. Would you recommend that I pre-define the length of the variable where I am trying to concatenate various strings of text? Thanks again for your help! 


Yes.  You should be able to define a length that is longer than any possible value your code will try to store in it.

 

In general you should define your character variables instead of letting SAS guess what length they need.  When it guesses it will usually guess a length of either 8 or 200 bytes when there is no other clear basis for it to choose something else.  In your example it is probably choosing $200 and that is ending up being too short.

yabwon
Onyx | Level 15

You need to defile proper length for expected value of 

qi104

 I guess something like:

length qi104 $ 500;

before line with CATS() function should help.

 

B.

_______________
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



ballardw
Super User

LOG.

Show the LOG.

"Similar" is not the same as Identical.

Data might be needed but the CATS function is not going to do much of anything except remove leading and trailing spaces when values are combined. A result of blank means that you gave the function blank input or tried to assign a character value to a numeric variable that couldn't be converted to numeric.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 10 replies
  • 1075 views
  • 3 likes
  • 5 in conversation