A question in terms of macro program

Reply
Frequent Contributor
Posts: 112

A question in terms of macro program

Hello everyone,

 

I was assigned a question in terms of macro program but I did not really get the difference between a macro variable and a macro program, and I did not understand what the question wanted. Would you mind explaining this question a bit for me?

Thank you! Should I just modify the code provided and let it read in the data of the data set  tb2000(which provided in the question)?

A bit confused what this was asking about. Thanks much!!

 

    • Modify the following macro so that it can read in data from any lib, any dsn, and for any number of observations. Use keyword parameters lib, dsn, obs, with defaults of WORK, _LAST_, and 5, respectively. Name the macro pam. Execute your macro on the SAS data set tb2000, printing out the first 8 records.

 

     %macro p5am;

      title "Listing of first 5 records from library &lib, member &dsn,";

      title2 "on &sysday, &sysdate..";

      proc print data=&lib..&dsn (obs=5);run;

      title;

%mend p5am;

Super User
Posts: 6,785

Re: A question in terms of macro program

The part you missed is "keyword parameter".  If you are going to write macros you absolutely need to know what this means.  You absolutely need to know what a macro variable is.  To give you a small push in the right direction, this will be a part of the answer:

 

%macro pam (lib=);

Frequent Contributor
Posts: 112

Re: A question in terms of macro program

Posted in reply to Astounding

Thank you, I will keep trying.

I just print the original data set with the codes without macro variables and did not know where should I turn to macro...lol

Super User
Super User
Posts: 8,127

Re: A question in terms of macro program

Google is your friend.

 

Make sure you understand what a macro variable is.

https://www.google.com/search?q=%40sas.com+macro+variable

 

Then learn about what a macro is. How to define one and how to call it.

https://www.google.com/search?q=%40sas.com+macro+definition

 

Then to question turns into how to put these into action.  One hint is that the example included in the question includes all of the actual SAS code you will need.  Your task is to modify the macro definition so that it can be used to generate the right code based on how the user calls.  Of course to do that right you need to understand the SAS code in the example macro.

Frequent Contributor
Posts: 112

Re: A question in terms of macro program

I see. Thank you very much!!

Frequent Contributor
Posts: 112

Re: A question in terms of macro program

My code is as below:

 

%MACRO pam(lib=WORK, dsn=_LAST_,obs=5);
%let dirdata=/folders/myfolders;
infile "&dirdata/&lib/tb2000.sas.7bdat";

proc print &pam;
title "Listing of first 5 records from library &lib, member &dsn,";
title2 "on &sysday, &sysdate..";
%mend

The LOG is as below:

1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         
 74         %MACRO pam(lib=WORK, dsn=_LAST_,obs=5);
 75         %let dirdata=/folders/myfolders;
 76         infile "&dirdata/&lib/tb2000.sas.7bdat";
 77         
 78         proc print &pam;
 79         title "Listing of first 5 records from library &lib, member &dsn,";
 80         title2 "on &sysday, &sysdate..";
 81         %mend
 82         
 83         
 84         
 85         
 86         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 WARNING: Extraneous text on %MEND statement ignored for macro definition PAM.
 87         ODS HTML CLOSE;
 88         &GRAPHTERM; ;*';*";*/;RUN;QUIT;
 89         QUIT;RUN;
 90         ODS HTML5 (ID=WEB) CLOSE;
 91         
 92         ODS RTF (ID=WEB) CLOSE;
 93         ODS PDF (ID=WEB) CLOSE;
 NOTE: ODS PDF(WEB) printed no output. 
       (This sometimes results from failing to place a RUN statement before the ODS PDF(WEB) CLOSE statement.)
 94         FILENAME _GSFNAME;
 NOTE: Fileref _GSFNAME has been deassigned.
 95         DATA _NULL_;
 96         RUN;
 
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.01 seconds
       
 
 97         OPTIONS VALIDMEMNAME=COMPAT;
 98         OPTIONS NOTES STIMER SOURCE SYNTAXCHECK;
 99         

It seemed I should have added a "RUN" statement?

Was my definition to the macro "pam" correct?

I actually used a usual code and then tried to put macro variable in it.

Frequent Contributor
Posts: 112

Re: A question in terms of macro program

My latest attempt:

%MACRO pam(lib=WORK, dsn=_LAST_,obs=5);
%let dirdata=/folders/myfolders;
data tb2000;
infile "&dirdata/&lib/&dsn";
proc print data=tb2000;
run;

title "Listing of first 5 records from library &lib, member &dsn,";
title2 "on &sysday, &sysdate..";
%mend

Had no idea where had went wrong. The LOG showed:

 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         
 74         %MACRO pam(lib=, dsn=,obs=);
 75         %let dirdata=/folders/myfolders;
 76         data tb2000;
 77         infile "&dirdata/Programs/Week_6/tb2000.sas7bdat";
 78         proc print data=tb2000;
 79         run;
 80         
 81         title "Listing of first 5 records from library &lib, member &dsn,";
 82         title2 "on &sysday, &sysdate..";
 83         %mend
 84         
 85         %pam(lib=WORK, dsn=_LAST_,obs=5);
 WARNING: Extraneous text on %MEND statement ignored for macro definition PAM.
 86         
 87         
 88         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 101        
SAS Super FREQ
Posts: 9,373

Re: A question in terms of macro program

Hi:
This may help you get started: https://support.sas.com/resources/papers/proceedings13/120-2013.pdf
cynthia
Frequent Contributor
Posts: 112

Re: A question in terms of macro program

Posted in reply to Cynthia_sas

Thanks a lot!

Frequent Contributor
Posts: 112

Re: A question in terms of macro program

Posted in reply to Cynthia_sas

Well...I have no idea what to do next Smiley Sad

 

This is my code:

options MPRINT;

%MACRO pam;
%let dirdata=/folders/myfolders;
%let dirOUT=/folders/myfolders;
%let lib=Week_6;
libname Week_6 "&dirdata";
run;
data tb2000;
infile "&dirdata/Programs/Week_6/tb2000.sas7bdat";
proc print data=tb2000 obs=5; 
title "Listing of first 5 records from &lib, &dsn,";
title2 "on &sysday, &sysdate..";
run;
%mend pam;

%pam(&lib, &dsn, &obs);
proc print data=pam;
run;

The LOG is as below:

1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 72         
 73         options MPRINT;
 74         
 75         %MACRO pam;
 76         %let dirdata=/folders/myfolders;
 77         %let dirOUT=/folders/myfolders;
 78         %let lib=Week_6;
 79         libname Week_6 "&dirdata";
 80         run;
 81         data tb2000;
 82         infile "&dirdata/Programs/Week_6/tb2000.sas7bdat";
 83         proc print data=tb2000 obs=5;
 84         title "Listing of first 5 records from &lib, &dsn,";
 85         title2 "on &sysday, &sysdate..";
 86         run;
 87         %mend pam;
 88         
 89         %pam(&lib, &dsn, &obs);
 MPRINT(PAM):   libname Week_6 "/folders/myfolders";
 NOTE: Libref WEEK_6 was successfully assigned as follows: 
       Engine:        V9 
       Physical Name: /folders/myfolders
 MPRINT(PAM):   run;
 MPRINT(PAM):   data tb2000;
 MPRINT(PAM):   infile "/folders/myfolders/Programs/Week_6/tb2000.sas7bdat";
 
 NOTE: The infile "/folders/myfolders/Programs/Week_6/tb2000.sas7bdat" is:
       Filename=/folders/myfolders/Programs/Week_6/tb2000.sas7bdat,
       Owner Name=sasdemo,Group Name=sas,
       Access Permission=-rw-rw-r--,
       Last Modified=10Mar2018:22:36:26,
       File Size (bytes)=50176
 
 NOTE: 0 records were read from the infile "/folders/myfolders/Programs/Week_6/tb2000.sas7bdat".
 NOTE: The data set WORK.TB2000 has 1 observations and 0 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.01 seconds
       cpu time            0.00 seconds
       
 
 22: LINE and COLUMN cannot be determined.
 NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
 ERROR 22-322: Expecting a quoted string.  
 200: LINE and COLUMN cannot be determined.
 NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
 ERROR 200-322: The symbol is not recognized and will be ignored.
 MPRINT(PAM):   proc print data=tb2000 obs=5;
 WARNING: Apparent symbolic reference DSN not resolved.
 MPRINT(PAM):   title "Listing of first 5 records from Week_6, &dsn,";
 MPRINT(PAM):   title2 "on Sunday, 11MAR18.";
 MPRINT(PAM):   run;
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: PROCEDURE PRINT used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 WARNING: Apparent symbolic reference LIB not resolved.
 WARNING: Apparent symbolic reference DSN not resolved.
 WARNING: Apparent symbolic reference OBS not resolved.
 
 
 90         proc print data=pam;
 ERROR: File WORK.PAM.DATA does not exist.
 91         run;
 
 NOTE: The SAS System stopped processing this step because of errors.
 NOTE: PROCEDURE PRINT used (Total process time):
       real time           0.00 seconds
       cpu time            0.00 seconds
       
 92         
 93         
 94         
 95         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 108        

Any suggestions would be highly appreciated!

I have read the reference but kinda got stuck on how to modify this one...

I can tell from the topics that those key parameters the question wants,

but I did not know how to put them in my code and make the output looks meaningful...

got stuck a bit long and need to move to another question..though . 

Super User
Super User
Posts: 8,127

Re: A question in terms of macro program

I thought the goal of the macro was to print an EXISTING dataset.  Why would you add anything other than the PROC PRINT step to the macro code?  If you want to make a dataset for it to print create that before you call the macro.

 

 

To help see whether you macro code is right pretend you are the macro processor and replace all of the macro variable references with the macro variable's values. Type out what you think that will be and see if the syntax looks right to you.

 

How do you tell PROC PRINT what dataset to print? What is the syntax for that?

How can you limit how many observations from a data set that SAS uses? What syntax would you use for that?

 

Ask a Question
Discussion stats
  • 10 replies
  • 191 views
  • 4 likes
  • 4 in conversation