Hello,
I am running the macro below and working as it should until the last step where I sort and merge. When I call the macro in my code it does not create the new data set with my merged data. Is there something obvious in that part of the code I am missing? Thank you
%macro PT_expect(oldData=, newData=);
/* create a character increment var to merge on, recode to number values (they are still character types) */
data &oldData; set &oldData;
format unique_id_macro $5.;
length unique_id_macro $5;
unique_id_macro = _N_;
run;
/* converts id variable to character, and converts all neccesary score variables to numeric */
data newdat;
set &oldData;
format unique_id_macro $5.;
length unique_id_macro $5;
unique_id_macro = _N_;
/* All Pt Expect vars */
array pt_expect_array (19) tka_expectations1-tka_expectations19;
array newvars (19) var1-var19;
format var1-var19 5.;
do i=1 to 19;
if pt_expect_array{i} in: ('<b>Back to normal</b> or complete improvement') then newvars{i} = 4;
else if pt_expect_array{i} in: ('Not back to normal, but a lot of improvement') then newvars{i} = 3;
else if pt_expect_array{i} in: ('Not back to normal, but a moderate amount of improvement') then newvars{i} = 2;
else if pt_expect_array{i} in: ('Not back to normal, but a little improvement') then newvars{i} = 1;
else if pt_expect_array{i} in: ('I do not have this expectation, or this expectation does not apply to me') then newvars{i} = 0;
else if pt_expect_array{i} in: (' ') then newvars{i} = .;
end;
run;
data newdat2;
set newdat;
array pt_expect_post_array (19) tka_satis1-tka_satis19;
array newvariable (19) variable1-variable19;
format variable1-variable19 5.;
do i=1 to 19;
if pt_expect_post_array{i} in: ('Complete Satisfaction') then newvariable{i} = 3;
else if pt_expect_post_array{i} in: ('Partial Satisfaction') then newvariable{i} = 2;
else if pt_expect_post_array{i} in: ('Dissatisfaction') then newvariable{i} = 1;
else if pt_expect_post_array{i} in: ('Not Applicable') then newvariable{i} = 0;
else if pt_expect_post_array{i} in: (' ') then newvariable{i} = .;
end;
*if reported a "0" for pre then post is to be discarded per guidelines;
data newdat3;
set newdat2;
array newvars (19) var1-var19;
array newvariable (19) variable1-variable19;
do i=1 to 19;
if newvars{i} = 0 then newvariable{i} = .;
end;
run;
data PT_expect_scores;
set newdat3;
knee_PT_expect_pre_n= N(of var1-var19);
knee_PT_expect_pre_nmiss= nmiss(of var1 - var19);
knee_PT_expect_pre_score = sum(of var1-var19);
knee_PT_expect_pre_scaled = knee_PT_expect_pre_score*1.31578947;/*per guidelines scale to out of 100--100/76(max score) */
knee_PT_expect_post_n= N(of variable1-variable19);
knee_PT_expect_post_nmiss= nmiss(of variable1 - variable19);
knee_PT_expect_post_score = sum(of variable1-variable19);
knee_PT_expect_post_scaled = knee_PT_expect_post_score*1.75438596;/*per guidelines scale to out of 100--100/57(max score)*/
run;
/* sort and merge them back on the ID variable */
proc sort data=PT_expect;by unique_id_macro; run;
proc sort data=&oldData;by unique_id_macro; run;
data &newData;
merge &oldData PT_expect_scores(keep=unique_id_macro
knee_PT_expect_pre_scaled knee_PT_expect_post_scaled);
by unique_id_macro;
drop unique_id_macro;
run;
%mend PT_expect;
It is working as it should until the last step. I checked the data set " PT_expect_scores" and it has been created as planned. So, the error must be in my sort and merge statement but I cannot see what I have done incorrectly. Any help would be greatly appreciated. Thank you
And exactly what does the macro call look like? If you have a value for the macro variable NEWDATA that is invalid as a data set name is the most likely cause from what I can see.
I suggest you look very closely at your log.
You have a comment:
*if reported a "0" for pre then post is to be discarded per guidelines;
that I suspect is generating an error of some sort, even though it may not affect the creation of Newdat3.
Use of that style of comment inside a macro is potentially a problem as the macro language does not recognize the *<text>; style of inline comment and may cause problems. If you must use comments like that the proper form in the macro language is %*<text>;
And you have at least 2 unneeded data steps. All of those array statements and code can be in a single data set. You are not removing or adding records.
First, place this command at the start of your program and run the macro again.
options mprint;
Show us the log for this run of the macro. Include the entire code as it appears in the log, plus all NOTES, WARNINGS and ERRORs. Do not chop anything out of the log.
When providing the log, it is critical that you maintain the formatting of the log so we can see it exactly as SAS showed it to you, making it easier for us to use. To maintain the formatting of the log, click on the </> icon and paste the log as text into the window that appears. DO NOT SKIP THIS STEP.
You didn't use the INSERT CODE button when pasting the lines from the log.
If that is your full log then your macro does not appear to be doing anything at all.
Or perhaps the include file has unbalanced quotes (or parentheses or comment blocks etc.).
You should really set the MPRINT option after the %INCLUDE just in case that program turned it off. (and check that the macro itself doesn't turn it off).
%INCLUDE 'C:\Users\abcd\Desktop\SAS Code\Pt expectations Macro_working.sas';
options mprint;
%pt_expect(oldData= scored, newData= scored2)
You also seem to have submitted over 16K lines of code at this point. It might be time to start and new SAS session and see if that makes it start working properly.
%INCLUDE 'C:\Users\abcd\Desktop\SAS Code\Pt expectations Macro_working.sas' /notes notes2;
options mprint;
%pt_expect(oldData= scored, newData= scored2)
My bad it should have been
%include <filename>/source2;
@GS2 wrote:
377 %INCLUDE 'C:\Users\abcd\Desktop\SAS Code\Pt expectations Macro_working.sas' /notes notes2;
WARNING 14-169: Assuming the symbol NOTE was misspelled as NOTES.
ERROR: Invalid option name NOTE.
ERROR: Invalid %INCLUDE option NOTES NOTES2. %INCLUDE statement will not be executed.
378 options mprint;
379 %pt_expect(oldData= scored, newData= scored2)
-
180
WARNING: Apparent invocation of macro PT_EXPECT not resolved.
ERROR 180-322: Statement is not valid or it is used out of proper order.
BY any chance do you mean the option SOURCE2 instead of NOTES2?????????
I don't see either NOTE or NOTE2 as options for the %include statement under Windows, Unix or Z/OS
Because of that error the Macro code isn't even submitted. If you have data set around with the right name it is likely left over from a test or other step. The &Newdata would not be created because that data step never executed at all because the macro was not compiled.
And exactly what does the macro call look like? If you have a value for the macro variable NEWDATA that is invalid as a data set name is the most likely cause from what I can see.
I suggest you look very closely at your log.
You have a comment:
*if reported a "0" for pre then post is to be discarded per guidelines;
that I suspect is generating an error of some sort, even though it may not affect the creation of Newdat3.
Use of that style of comment inside a macro is potentially a problem as the macro language does not recognize the *<text>; style of inline comment and may cause problems. If you must use comments like that the proper form in the macro language is %*<text>;
And you have at least 2 unneeded data steps. All of those array statements and code can be in a single data set. You are not removing or adding records.
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.