BookmarkSubscribeRSS Feed
struppi1979
Calcite | Level 5

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

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

struppi1979
Calcite | Level 5

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

Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

"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. 

Kurt_Bremser
Super User

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.

struppi1979
Calcite | Level 5

... solved.. this works fine , i should have a better look at the results...  Thanks to all

 

index(parameter,"&variable")

 

I had a mental error

 

Kurt_Bremser
Super User

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.

struppi1979
Calcite | Level 5

yes sorry... my mistake. 😞

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 992 views
  • 0 likes
  • 3 in conversation