BookmarkSubscribeRSS Feed
Kathy_JSG
Calcite | Level 5

I wrote a program that reads in data that looks like this:

IDTypeGenderEthnicity_start_endrank
1001549PMale124-Mar-1723-Mar-192
1001549CSMale116-Mar-1514-Jun-171

and analyzes that data to create daily population data that will look like this:

rankGenderEthnicitySnapshotTypecount
1Male01-Apr-17CS1

Rather than running that program 365 times, changing the date each time, I thought I could put it into a macro. But I can't figure out how to increase the date each iteration. Here's the program that I have written so far.

 

%let date = '01apr2017'd;

*read in the programs data and keep only the programs that were active on the snapshot date;

*sort by client id, then by the rank of their programs that were active on that day;

proc sort data=Intakes1 out = a;

where _start <= &date <= _end;

by id rank;

run;

 

*for each client, keep the program that is ranked highest on that date;

data b;

set a;

by id;

if first.id;

 

snaphot = &date;

count = 1;

format snapshot date9.;

run;

 

*sort to prepare for proc summary;

proc sort;

by rank gender ethnicity;

run;

 

*aggregate the program records to get a total count of clients, categorized by their highest ranked program;

proc summary nway;

class rank gender ethnicity;

var count;

id snapshot type;

output out=c sum=;

run;

 

*add the results of c to the daily dataset;

*the daily dataset will be used to create a fiscal year average of clients, analyzed by type of program/gender/ethnicity;

data daily (drop = _type_ _freq_);

set daily c;

run;

 

*delete the work datasets and start again;

proc datasets library = work nolist;

delete a b c;

quit;

run;

 

I'd like the macro to repeat for each day in the fiscal year. I thought I could start with

     %macro repeat(n);

     %do I=1 %to &n.;

and end with

     %end;

     %mend repeat;

     %repeat(365);   (although would have to modify that during a leap year);

but I just can't figure out how to manage the date to increase it each time and use it through all of those proc and data steps. 

 

Thanks for any help that you can provide! I am using SAS 9.4.

 

 

 

 

1 REPLY 1
Astounding
PROC Star
I would suggest adding observations and eliminating macro language. For example:

data want;
set have;
do date=_start to _end;
output;
end;
drop _start _end;
run;

Then you can add DATE to the list of BY variables and process the data just once.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 325 views
  • 0 likes
  • 2 in conversation