BookmarkSubscribeRSS Feed
jamieflynn
Calcite | Level 5

Hi everyone, 

 

I have the following code where I have to print a list of all Models and the corresponding Prices that were not purchased (files are attached):

 

/*Checking contents*/
title "Contents of Inventory dataset"; 
proc contents data=BSTA445.inventory; 
run; 
title;

title "Contents of Purchase dataset"; 
proc contents data=BSTA445.purchase; 
run; 
title;

/*Sort Inventory and Purchase datasets by Model*/
proc sort data=BSTA445.inventory;
by Model;
run;

title "Sorted Inventory dataset";
proc print data=BSTA445.inventory;
run;
title;


proc sort data=BSTA445.purchase;
by Model;
run;

title "Sorted Purchase dataset";
proc print data=BSTA445.purchase;
run;
title;

/*Merge 2 sorted datasets*/
data notpurchased;
	merge BSTA445.inventory (in=InInvent) BSTA445.purchase (in=inPurch);
	by Model;
	if InInvent and not InPurch;
	keep Model Price;
run;

title "List of unpurchased Models and respective Prices";
proc print data=notpurchased noobs;
run;
title;

 

 

I want to convert the above program into a Macro called bike_sale with the following three macro parameters:

  • DSN1 as “Inventory"
  • DSN2 as “Purchase”
  • Sold as “whether the bike was sold or not"
  • Use Mprint option.
  • Run MACRO as: %bike_sale(inventory, purchase, not)

I'm taking a SAS programming course and this is my very first programming class. I understand that to use Macro, I'd have to replace any texts containing "Inventory" with &DSN1, "Purchase" with "&DSN2" in the above code. However, can anyone please explain to me where I am supposed to replace "Sold" with? 

 

Thank you a lot for your help!

 

 

4 REPLIES 4
Astounding
PROC Star

My interpretation:

 

Your final DATA step uses in= variables to come up with a list of models that were not sold.

 

Alternatively, you could construct a DATA step that locates models that were sold (with different logic for the subsetting IF statement).

 

So depending on whether the parameter SOLD is Y or N, you would need to make the macro choose which subset to create, and which logic to apply using the in= variables.

PaigeMiller
Diamond | Level 26

However, can anyone please explain to me where I am supposed to replace "Sold" with?

 

Well, that's something that isn't clearly explained in the problem statement, and you have to figure out from looking at the two data sets and applying simple logic to what you see.

 

I'm taking a SAS programming course and this is my very first programming class


So this issue about SOLD is not a programming issue at all, it is an understanding of the problem issue. Forget SAS and programming, how would you determine if a bicycle is sold or not? Once you can answer that, then you can do some programming on this matter.

 

Side comment: if this is an assignment to help you learn to use macros, this is a particularly poor example of when to use macros in the first place, and your instructor could easily provide more meaningful use of macros. The whole program could be written without macros; and even if there must be macros, then the reason you would use macros is because something changes in each invocation of the macros, and that most likely will be BSTA445 to some other library name. But anyway ... I hate this problem ... your instructor is misleading you about the proper use of macros.

--
Paige Miller
Shmuel
Garnet | Level 18

Relating to your request: " explain to me where I am supposed to replace "Sold" with? "

That's connected to the merge step: you need change conditionally the statement: 

if InInvent and not InPurch;

your macro should look like:

%macro bike_sale(dsn1, dsn2, sold=);
    /* sold = YES or sold=NO */
    proc sort data=&dsn1; by model; run;
    proc sort data=&dsn2; by model; run;

   data to_print;
	merge &dsn1 (in=in1) 
                   &dsn2 (in=in2);
	by Model;
	%if &sold = YES %then %do;
              if in1 and in2;
        %end; %else
	%if &sold = NO  %then %do;
              if in1 and not in2;
        %end;
	keep Model Price;
run;

/* enter a title according to sold YES or NO using %if ... */
   proc print data=to_print; 
   run;
%mend bike_sale;
%bike_sale(..... enter your argumnets ... );
Reeza
Super User

Does the code included do everything you want to do?

I see no indication of a SOLD variable being used anywhere. 

 

In general before you convert your code to a macro you should be ensuring you have code that works and does what you need. 

 

Once you have working code for one of your cases here's a tutorial on converting a working program to a macro

This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

And here are some other examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 504 views
  • 0 likes
  • 5 in conversation