DATA Step, Macro, Functions and more

macro for 'combinations'

Accepted Solution Solved
Reply
Contributor K_S
Contributor
Posts: 23
Accepted Solution

macro for 'combinations'

pt IDsexservice ID
12M22
22F34
22F77
24M47
24M24
845F23
2384F99
4905M96
95M52

 Could somebody help me create a macro that prints pt ID for any combination of sex and service ID? Service ID s go from 1 to 99.

I have an idea of how to do this but I wanna see if there is a better, more logical way.

Thank you in advance!

 


Accepted Solutions
Solution
‎03-24-2017 10:30 PM
PROC Star
Posts: 7,356

Re: macro for 'combinations'

You don't need a macro, but here is one way to do it without a macro and one way using a macro:

 

proc print data=have (where=(sex eq 'M' and service_id eq 22));
  var pt_id;
  by service_id sex ;
run;

/*or*/

%macro doit(sex,service_id);
  title "Patients with Gender = &sex. and Service_ID = &Service_ID";
  proc print data=have (where=(sex eq "&sex" and service_id eq &service_id));
    var pt_id;
  run;
%mend doit;

%doit(M,22)

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
PROC Star
Posts: 7,356

Re: macro for 'combinations'

Not sure what you want. Do you want 99 records for every pt_id?

 

Art, CEO, AnalystFinder.com

 

Contributor K_S
Contributor
Posts: 23

Re: macro for 'combinations'

hmmm....nope. I have thousands of patients and 99 codes and 2 sexes.
so for every combination of sex and code, I want to print the pt IDs
for example I want to know the IDs of all the patients who are M and are in
for a 64 ( which is psychiatric services), and so on and so forth. A macro
could do this..

##- Please type your reply above this line. Simple formatting, no
attachments. -##
PROC Star
Posts: 7,356

Re: macro for 'combinations'

If I correctly understand, you may only need something like:

 

proc sort data=have;
  by service_id sex ;
run;

proc print data=have;
  var pt_id;
  by service_id sex ;
run;

Art, CEO, AnalystFinder.com

 

Contributor K_S
Contributor
Posts: 23

Re: macro for 'combinations'

hmm...no, not what I am looking for. It has to be a macro that would allow
me to randomly pick combinations of sex and service IDs.
Let's say today I want to get the IDs of all the males who are in for a 64,
but then tomorrow I want to look at women who are in for a 52.
I am sorry if I am not making a lot of sense. I am new to this and not
entirely sure what I am being asked to do. All I know is that it has to be
a macro that would allow me to get pt_IDs for any combinations of sex and
service id.


##- Please type your reply above this line. Simple formatting, no
attachments. -##
Solution
‎03-24-2017 10:30 PM
PROC Star
Posts: 7,356

Re: macro for 'combinations'

You don't need a macro, but here is one way to do it without a macro and one way using a macro:

 

proc print data=have (where=(sex eq 'M' and service_id eq 22));
  var pt_id;
  by service_id sex ;
run;

/*or*/

%macro doit(sex,service_id);
  title "Patients with Gender = &sex. and Service_ID = &Service_ID";
  proc print data=have (where=(sex eq "&sex" and service_id eq &service_id));
    var pt_id;
  run;
%mend doit;

%doit(M,22)

Art, CEO, AnalystFinder.com

 

Contributor K_S
Contributor
Posts: 23

Re: macro for 'combinations'

This is beautiful! Thank you!
You just helped me understand something I completely missed. Much
appreciated Smiley Happy

##- Please type your reply above this line. Simple formatting, no
attachments. -##
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 144 views
  • 1 like
  • 2 in conversation