BookmarkSubscribeRSS Feed
beverlyobeng
Calcite | Level 5

Hi I would like to create a macro that would allow me to perform rougly the same procedure but with different datasets. Below are two datasets I would like to create a macro do in one step.Any help would be greatly appreciated

 


*<-STAT COL1 -> ASSIGN DEMOGRAPHICS TO >=1lab @facX;
*>=1lab at Facility_X;
proc sort data=vs_Fac_pat; by cityno; run; */;
proc sort data=plwha; by cityno; run;
data Fac_demo;
merge vs_Fac_pat (in=a) plwha (in=b) ;
by cityno;
if a=1;
run; */Bright: 1493 Elm:1433;

 

proc freq data=Fac_demo order=formatted;
Tables sex newrace livingage&year. risk/missing;
format sex $newsex. newrace $race5cat. livingage&year. newage. risk $newrisk.;
run; */COL1 ***;

 

*prepare col1 output;
proc freq data=Fac_demo order=formatted;
table sex /missing out=col1_sex (drop=percent);
table newrace /out=col1_race(drop=percent);
table livingage&year. /out=col1_age(drop=percent);
table risk /out=col1_risk(drop=percent);
format sex $newsex. newrace $race5cat. livingage&year. newage. risk $newrisk.;
run;

 

proc freq data=Fac_demo order=formatted;
table sex / out=col1_sex (drop=percent);
format sex $newsex. newrace $race5cat. livingage&year. newage. risk $newrisk.;
run;

 

*prepare col1 output;
proc freq data=Fac_demo order=formatted;
table sex /out=col1_sex (drop=percent);
table newrace /out=col1_race(drop=percent);
table livingage&year. /out=col1_age(drop=percent);
table risk /out=col1_risk(drop=percent);
format sex $newsex. newrace $race5cat. livingage&year. newage. risk $newrisk.;
run;

 

proc transpose data=col1_sex out=col1_sex2; id sex; var count; run;
data col1_sex3 (drop = _LABEL_ _NAME_ ); set col1_sex2; run;

 

proc transpose data=col1_race out=col1_race2; id newrace; var count; run;
data col1_race3 (drop = _LABEL_ _NAME_ ); set col1_race2; run;

 

proc transpose data=col1_age out=col1_age2; id livingage&year.; var count; run;
data col1_age3 (drop = _LABEL_ _NAME_ ); set col1_age2; run;

 

proc transpose data=col1_risk out=col1_risk2; id risk; var count; run;
data col1_risk3 (drop = _LABEL_ _NAME_ ); set col1_risk2; run;

 

data COL1_wf;
merge col1_sex3 col1_race3 col1_age3 col1_risk3 ;
run;


*<-STAT COL2-> ASSIGN DEMOGRAPHICS TO RETAINED (>=2labs 3 mo apart) ;
*Retained;
proc sort data=InCare1_pat; by cityno; run; */ citynos;
proc sort data=plwha; by cityno; run;


data InCare1_pat_demo;
merge InCare1_pat (in=a) plwha (in=b) ;
by cityno;
if a=1;
run; */Bright:1341 Elm: 1238;

 

proc freq data=InCare1_pat_demo order=formatted;
Tables sex newrace livingage&year. risk;
format sex $newsex. newrace $race5cat. livingage&year. newage. risk $newrisk.;
run;*/COL2 ***;

 

*prepare col2 output;
proc freq data=InCare1_pat_demo order=formatted;
table sex /out=col2_sex (drop=percent);
table newrace /out=col2_race(drop=percent);
table livingage&year. /out=col2_age(drop=percent);
table risk /out=col2_risk(drop=percent);
format sex $newsex. newrace $race5cat. livingage&year. newage. risk $newrisk.;
run;

 

proc transpose data=col2_sex out=col2_sex2; id sex; var count; run;
data col2_sex3 (drop = _LABEL_ _NAME_ ); set col2_sex2; run;

 

proc transpose data=col2_race out=col2_race2; id newrace; var count; run;
data col2_race3 (drop = _LABEL_ _NAME_ ); set col2_race2; run;

 

proc transpose data=col2_age out=col2_age2; id livingage&year.; var count; run;
data col2_age3 (drop = _LABEL_ _NAME_ ); set col2_age2; run;

 

proc transpose data=col2_risk out=col2_risk2; id risk; var count; run;
data col2_risk3 (drop = _LABEL_ _NAME_ ); set col2_risk2; run;

 

data COL2_wf;
merge col2_sex3 col2_race3 col2_age3 col2_risk3 ;
run;

 

3 REPLIES 3
Reeza
Super User

Are you asking how to repeat all of that code for different tables?

Or how to modify that code?

I'm not sure where macro's come in or what exactly you need help with....

LinusH
Tourmaline | Level 20

Not knowing if you have any special considerations. Do you just want to learn macro programming?

You have a quite extensive program, so it's hard for me to tell you how to adopt it (in a wise way).

Please see online doc, samples and there are hundreds of papers about macro programming.

 

Try to get started, and get back if you have a more specific question.

Data never sleeps
Reeza
Super User

I just posted this and I think it applies here as well. In your case I would also recommend another step - at the end of your macro you may want to remove any temporary data sets so they're not around for another macro iteration.

 

  1. Wrap your code in %macro/%mend and assign a name, eg:
  2. %macro summarize;
    
    *your sas code;
    
    %mend;
  3. Test that it works by calling it. 
    %summarize;
  4. Change macro to use a parameter  - the parameter is called dataset, and changes for each dataset you'd like to process
    %macro summarize(dataset);
  5. Change variable name in code to be parameter value, eg:
    proc means data=&dataset;
    ...
    run;
  6. Test again 
    %summarize(bpchild);
  7. Make sure it works for multiple cases.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 789 views
  • 0 likes
  • 3 in conversation