Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use Macro code? 1

Status
Not open for further replies.

RinaGreen

Technical User
Mar 8, 2005
31
US
Dear Experts,

I wrote code which looks pretty much the same in all three parts and differs just because of the different datasets. Data set names are the followings:

Retire
NoChoice
Choice

My code looks too long with 3 parts of it almost the same (actually other products(datasets) are coming in)

I feel I should use Macro but I have no idea how to start as I never did it before (although I read help on- line)

The following is code for the first part-retire:


data RetireDist;
set Retire;

gls=put(gl, $1.);
SuS=put(Su, $1.);
UrS=put(Ur, $1.);
hS=put(h, $1.);
Es=put(E, $1.);
fs=put(f, $1.);

if gl='1' THEN glS='Y';
else glS='N';

if su='1' OR
su='2' THEN suS='Y';
else suS='N';

if Ur='1' THEN UrS='Y';
else UrS='N';

if h='2' OR
h='3' THEN hS='Y';
else hS='N';

IF E='2' THEN ES='Y';
else ES='N';

if f='1' OR
f='2' THEN footExamS='Y';
else fS='N';

Bmem1 = substr(glS,1,1);
Bmem2 = substr(suS,1,1);
Bmem3 = substr(UrS,1,1);
Bmem4 = substr(hS,1,1);
Bmem5 = substr(ES,1,1);
Bmem6 = substr(fS,1,1);

/* Count # of Knowledge Questions for each patient out of the 6 of interest */

array Bcalc [6] $Bmem1-Bmem6;
Bcount=0;
do i=1 to 6;
if Bcalc(i) EQ 'Y' then Bcount=Bcount+1;
end;
drop i;
RUN;

proc freq data=RetireDist;
tables glS suS UrS hS ES fS;
title "Distribution Frequency of Yes answers for Specific questions for Retire";
run;

proc freq data= RetireDist;
tables Bcount;
title "Frequency of Count of Yes answers in the Retire";
run;



Data data NochoiceDist;
set NoChoice;
.
.
.
same code
run;
Data data choiceDist;
set Choice;
.
.
.
same code
run;


Could you give me a hand?

Thank you for your time and consideration!

Rina


 
Rina,
I'm not quite sure what you need to do by browsing the above code, but I assume that you want to see some stats on the number of Yes's to the questions.
My first question is why read the answers into a 'holding' var and than transfer this value to an array var to add the yes's up? I have changed your code a little. I presumed that the vars that you check are in char format so I left them in that format. I used your logic and set the new vars to either a 1 or a 0 (zero) Yes and NO. Although you never use the total (as the freq is run on the Y's and not your array) I included this in a total variable for you.

As to your question how to use this in a macro I have shown you as well.
Code:
%macro YourMacroName(DataSetNm=);
    %*** this is a Yes/No format ***;
    proc format;
        value yn
         0 = 'No'
         1 = 'Yes'
         ;
    run;

    %*** Your data  ***;
    %*** Yes=1 No=0 ***;
    data &DataSetNm.B;
        set &DataSetNm;

    if gl = '1' THEN 
       glS = 1;
    else
       glS = 0;

    if su in('1','2') THEN
       suS = 1;
    else 
       suS = 0;

    if Ur = '1' THEN
       UrS = 1;
    else 
       UrS = 0;

    if h in('2','3') THEN
       hS = 1;
    else 
       hS = 0;

    if E = '2' THEN
       ES = 1;
    else 
       ES = 0;

    if f in('1','2') THEN
       footExamS = 1;
    else 
       fS = 0;

    /* Count # of Knowledge Questions for each patient out of the 6 of interest that answered Y */

    TotalYes = sum(glS,suS,UrS,hS,ES,fS);
    format gls sus urs hs es fs yn.;
    run;

    title "Distribution Frequency of Yes answers for Specific questions for the &DataSetNm dataset";
    proc freq data = &DataSetNm.B;
       tables glS suS UrS hS ES fS;
    run;
%mend YourMacroName;
to invoke or use this macro all you have to do is this:
Code:
%YourMacroName(dataset=retire);
if your want to do multiple datasets just execute the macro using the new dataset name. If you wanted to get fancy, you could modify this macro to accept a list of datasets, but I dont think you need to do that. Another note the proc format could be executed once outside the macro (before) but I left it in to show you how its done.

I hope that this helps you,
Klaz
 
Good answer there Klaz.

Rina - As a tip, I recommend following the same procedure for building macros that you have here, ie - write the code to run once, get it working, then convert it to run as a macro.
When I build code like this with a view to turn it into a macro, I tend to pick out the paramters and set up macro variables (the &xxxx bits) for them as I go and set them at the top of the code using %let statements to make it easier. Then all I need to do is put in the %MACRO and %MEND statements and remove the %LET statements to make it work.

You'll notice that Klaz has put a . after some of the macro variables, this is to terminate the macro variable, and the . doesn't appear in the compiled code. If you want the dot, for instance if the macro variable is a libname, then you put in 2 dots, like this:-
Code:
  Data &libnm..%dset. ;

Macros are actually pretty easy, though they can be pretty intimidating at first. The bulk of the use of them is exactly as Klaz explained above. There is a host of other useful little bits and pieces, but they are pretty rarely used.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top