BookmarkSubscribeRSS Feed
jillf78
Calcite | Level 5

I have to make a counter by two variables, Month and City.  I need it to count up the days per month per city and right now it is counting up only per month so I end up with 62 days for august for the cities combined instead of 31 for each city and that is happening for each month.  Is there a way to code it to start from one for every month and city? Thanks

Here is my code:

data ozone; set ozone; run;
proc sort data = ozone; by Month; run;
data ozone; set ozone; by Month; if first.Month then Day = 1; else Day+1; run;

proc print data = ozone; title 'New Variable - Day';

1 REPLY 1
lrudolphi
Obsidian | Level 7

It seems like a matter of adding your city variable to the sort and by statements. I mocked up some data and used it to build the SAS code below. The output would be a unique city-month listing with day count. Sorting by city and month, and then using first.month will ID the first instance of month for that sort pair and reset the counter to 0. All of this is assuming your data is set up such that it's a unique city, month, and day record.

 

/*Make dummy data for testing*/
data ozone (keep=city month);
	do city=1 to 3;
		do month=1 to 3;
			do day=1 to 31;
				output;
			end;
		end;
	end;
run;

/*Sort on the variables by which you want to aggregate*/
proc sort data=ozone;
	by city month;
run;

/*Make a counter - will reset at the start of every city month combo*/
data counter;
	set ozone;
	by city month;

	if first.month then day_count=0;
	day_count + 1;

	/*Can use if you want to output a single city-month record with day count*/
	if last.month then output;
run;

proc print data=counter; run;

Output looks like the attached screen grab.

Capture.JPG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1 reply
  • 983 views
  • 0 likes
  • 2 in conversation