Dear users,
i wrote a Macro to select some of a long list of blood levels (over 100.000).
especially here glucosis.
The Problem is how to insert correctly the macro variable ... this part does not work 😞 to select only the glucosis
parameter in ('&variable')
here the complete code
options mcompilenote=all; %macro sort_macr (variable); DATA alle.&variable._T0 (keep =studie bmid bmdt_int bmankli_d bmankli_u abndat parameter wert Uhrzeit zeit minuten &variable._ank_dat &variable._ank_wert &variable._Tag1_wert &variable._ank_comment &variable._Tag1_comment); FORMAT &variable._ank_dat DDMMYY10. &variable._ank_wert &variable._Tag1_wert $50. &variable._ank_comment &variable._Tag1_comment $50. Uhrzeit HHMM5. minuten 4.; SET alle.merge_ank_lab_1; Uhrzeit=timepart(zeit); minuten=INTCK('minute',bmankli_u,Uhrzeit); if bmankli_d = abndat; if wert not in('.','n.nwb.','neg','neg.'); if 0 <= minuten<=70 and not missing (bmankli_d) and not missing (bmankli_u) and parameter in ('&variable') then do; &variable._ank_dat=abndat; &variable._ank_wert=wert; &variable._ank_comment = 'Abnahme 0 bis 70 min nach Aufnahme'; end; RUN; %mend; %sort_macr(Glucose)
Thank you very much
Use double quotes around macro variables otherwise they do not resolve:
parameter in ("&variable.")
And don't forget to always include the decimal place, you may get away with it, but best practice to always include it. I do also wonder why you need a macro for this at all. Its just a datastep where you have extracted one item, why not do all items in one datastep, no need for any macro code or the associated loops or merges needed later on because of that process.
hey... this macro is made for more than one blood levels (hba1c, cholesterin ...)
so it is much easier do do changes only once , and not on several parts in program.
but your solution does not work. 😞 the program still selects all blood levels
And work on consistent visual formatting and a consistent coding style.
Compare this:
%let variable=Glucose;
data alle.&variable._t0 (keep=
studie bmid bmdt_int bmankli_d bmankli_u abndat parameter wert
uhrzeit zeit minuten &variable._ank_dat &variable._ank_wert
&variable._tag1_wert &variable._ank_comment &variable._tag1_comment
);
format
&variable._ank_dat ddmmyy10.
&variable._ank_wert
&variable._tag1_wert
&variable._ank_comment
&variable._tag1_comment
$50.
uhrzeit hhmm5.
minuten 4.
;
set alle.merge_ank_lab_1;
uhrzeit = timepart(zeit);
minuten = intck('minute',bmankli_u,uhrzeit);
if bmankli_d = abndat;
if wert not in('.','n.nwb.','neg','neg.');
if
0 <= minuten <= 70 and not missing(bmankli_d)
and not missing(bmankli_u) and parameter = "&variable."
then do;
&variable._ank_dat = abndat;
&variable._ank_wert = wert;
&variable._ank_comment = 'Abnahme 0 bis 70 min nach Aufnahme';
end;
run;
with your original code.
"hey... this macro is made for more than one blood levels (hba1c, cholesterin ...)"
So? If you have an LB domain dataset, then you can process all blood levels in one datastep, using the same code, no need to break anything out into individual tests? If the condition changes based on param, then use an if block or a select block. Macro is never needed.
In terms of your response, why would you expect it to return only a set amount of records, I cannot see from a glance anywhere where you restrict the data, e.g. a where clause, so of course it would return everything. The only place its used is in:
and parameter in ('&variable')
But that doesn't conditionally output anything.
As a note for future, stating "it doesn't work" will not get you much help back. Need to see code, logs, test data in the form of a datastep.
Since you want to use &variable as part of SAS names, it can't be a list.
So using
in ("&variable")
is misleading and therefore bad.
Use
= "&variable"
instead.
Next, run this code:
%let variable=Glucose;
DATA alle.&variable._T0 (keep =studie bmid bmdt_int bmankli_d bmankli_u abndat parameter wert Uhrzeit zeit minuten &variable._ank_dat &variable._ank_wert &variable._Tag1_wert &variable._ank_comment &variable._Tag1_comment);
FORMAT &variable._ank_dat DDMMYY10.
&variable._ank_wert &variable._Tag1_wert $50.
&variable._ank_comment &variable._Tag1_comment $50.
Uhrzeit HHMM5.
minuten 4.;
SET alle.merge_ank_lab_1;
Uhrzeit=timepart(zeit);
minuten=INTCK('minute',bmankli_u,Uhrzeit);
if bmankli_d = abndat;
if wert not in('.','n.nwb.','neg','neg.');
if 0 <= minuten<=70 and not missing (bmankli_d) and not missing (bmankli_u) and parameter = "&variable." then do;
&variable._ank_dat=abndat;
&variable._ank_wert=wert;
&variable._ank_comment = 'Abnahme 0 bis 70 min nach Aufnahme';
end;
RUN;
and have a look at the log. Macro development must always start with working Base SAS code, so debug this until it works.
... solved.. this works fine , i should have a better look at the results... Thanks to all
index(parameter,"&variable")
I had a mental error
So parameter is actually a list of values, and &variable one of those? That would have been good to know from the start. That's why we always ask for properly presented example data, so we can follow Maxim 3.
yes sorry... my mistake. 😞
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.