BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10

Variables

MonthYr is a date variable

lbl is a char

cnt is a numeric

data test;

MonthYt=step_compl_dt;

if step_cd='111' then do; lbl='Modification';cnt=1;end;else

if step_cd='222' then do; lbl='Liquidation';cnt=1;end;

format MonthYr monyy7.;

drop ln_no;

run;

Sample Output

MonthYr                        lbl                                  cnt

Feb2014                         Modification                    1

Feb2014                         Modifcation                     1

Mar2014                         Liquidation                         1

Mar2014                         Modifcation                     1

Apr2014                         Modification                    1

May2014                         Liquidation                     1

May2014                         Modification                    1

Jun2014                         Modifcation                     1

The repeating months are based on the ln_no which has been dropped

When I attempt to do an agregate total

proc sql;

create table tes2 as

select MonthYr,lbl,sum(cnt) as cnt1

from test1

group by MonthYr,lbl

having sum(cnt);

quit;

Do you see anything wron with this proc statement.  I am getting repeats of the MonthYr and some totals

I should be getting something like

MonthYr                    Modification   Liquidation

Feb2014                         2

Mar2014                          1                    1             

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

SQL aggregations doesn't recognize formats on the date, you'll have to actually convert it to monyy7 via a text variable, or put them all as the same date and then run your SQL code.

Use either MonthYT2 or MonthYT3 in your code to get what you want.  I'd recommend MonthYT3 as it will still sort properly, while MonthYT2 will sort alphabetically.

data test;

MonthYt=step_compl_dt;

MonthYT2=put(step_compl_dt, monyy7.);

MonthYT3=intnx('month', step_compl_dt, 0, 'b');

if step_cd='111' then do; lbl='Modification';cnt=1;end;else

if step_cd='222' then do; lbl='Liquidation';cnt=1;end;

format MonthYr monyy7.;

drop ln_no;

run;

View solution in original post

3 REPLIES 3
ballardw
Super User

I suspect that if you look at the values of MonthYr using a format like MMDDYY10 you will see that they are different days of the month.

You want to make sure they are using the same DAY of month as group by doesn't look at formatted value.

Jagadishkatam
Amethyst | Level 16

Please try,

proc sql;

create table tes2 as

select  count(MonthYr) as cnt ,MonthYr, lbl/*, sum(cnt) as cnt1    */

from have

group by MonthYr,lbl

/*having sum(cnt)*/;

quit;

proc transpose data=tes2 out=trans;

by monthyr;

id lbl;

var cnt;

run;

Thanks,

Jag

Thanks,
Jag
Reeza
Super User

SQL aggregations doesn't recognize formats on the date, you'll have to actually convert it to monyy7 via a text variable, or put them all as the same date and then run your SQL code.

Use either MonthYT2 or MonthYT3 in your code to get what you want.  I'd recommend MonthYT3 as it will still sort properly, while MonthYT2 will sort alphabetically.

data test;

MonthYt=step_compl_dt;

MonthYT2=put(step_compl_dt, monyy7.);

MonthYT3=intnx('month', step_compl_dt, 0, 'b');

if step_cd='111' then do; lbl='Modification';cnt=1;end;else

if step_cd='222' then do; lbl='Liquidation';cnt=1;end;

format MonthYr monyy7.;

drop ln_no;

run;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 847 views
  • 6 likes
  • 4 in conversation