BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

Hello all-

Hope everybody had a good holiday!

I have a group of data that consistes of hospital wards, the year and month in separate columns, and some other various data.

In total there are now 10 units. What I want to do is combine the last year/month into a date value that has all the units accounted for.

So that,

For October, all the units were accounted for, but in November, only 7 units have entries thus far so what I want to do is use the October data in this case until all the November data is accounted for.

The query so far I have that successfully creates a date..everything I have tried results in a SELECT or HAVING error.

proc sql;

select max(input(compress(put(a.year,8.)||put(a.month,8.)),YYMMN6.)) as month2 format MONYY5.

from DEVICE_DAYS as A;

quit;

Some data is below.

Any suggestions greatly appreciated-

Lawrence

Query1
Locationmonthyear
11I102011
8E102011
M10102011
Z4I102011
M07102011
7N102011
M9I102011
13I102011
M6I102011
MIN102011
8E112011
M9I112011
13I112011
11I112011
M07112011
M6I112011
M10112011
1 ACCEPTED SOLUTION

Accepted Solutions
Peter_C
Rhodochrosite | Level 12

did you want a monYYYY date in a macro variable for the latest month in which there are 10 rows ?

try

proc sql noprint ;

    select  max(period) format=monyy7.

      into :latest_full_period

      from (

            select mdy( month,1,year)  as period

              from have

          group by month,year

            having count(*) eq 10

           )

;

%put (latest_full_period)=(&latest_full_period) ;

quit;

View solution in original post

6 REPLIES 6
art297
Opal | Level 21

Not sure if I understand correctly, but I think that you are looking for something like:

data have;

  input Location $ month year;

  cards;

11I          09          2011

8E          09          2011

M10          09          2011

Z4I          09          2011

M07          09          2011

7N          09          2011

M9I          09          2011

13I          09          2011

M6I          09          2011

MIN          09          2011

11I          10          2011

8E          10          2011

M10          10          2011

Z4I          10          2011

M07          10          2011

7N          10          2011

M9I          10          2011

13I          10          2011

M6I          10          2011

MIN          10          2011

8E          11          2011

M9I          11          2011

13I          11          2011

11I          11          2011

M07          11          2011

M6I          11          2011

M10          11          2011

M07          12          2011

M6I          12          2011

M10          12          2011

M07          12          2012

M6I          12          2012

M10          12          2012

;

proc sql;

  create table want as

    select *

      from (select *

              from have

                   group by month,year

                     having count(*) eq 10)

        having year=max(year) and month=max(month)

  ;

quit;

_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

Art-I think you are getting there but when I ran the code it didn't seem to function properly (or it return no values)

After some fiddling I came up with this..

proc sql;

select sum(1) as counter, input(compress(put(a.year,8.)||put(a.month,8.)),YYMMN6.) as month2

INTO :count1, :month1

from CHILLE.DEVICE_DAYS as A

group by calculated month2

having counter=10 and Max(year) and max(month)

ORDER BY month2 DESC;

quit;

That gives me the last month in a macro variable that I need. Not the most elegant solution but works for now..

Thanks for your help.

Lawrence

Ksharp
Super User

Maybe you need this:

data DEVICE_DAYS;
  input Location $ month year;
  cards;
11I          09          2011
8E          09          2011
M10          09          2011
Z4I          09          2011
M07          09          2011
7N          09          2011
M9I          09          2011
13I          09          2011
M6I          09          2011
MIN          09          2011
11I          10          2011
8E          10          2011
M10          10          2011
Z4I          10          2011
M07          10          2011
7N          10          2011
M9I          10          2011
13I          10          2011
M6I          10          2011
MIN          10          2011
8E          11          2011
M9I          11          2011
13I          11          2011
11I          11          2011
M07          11          2011
M6I          11          2011
M10          11          2011
M07          12          2011
M6I          12          2011
M10          12          2011
M07          12          2012
M6I          12          2012
M10          12          2012
;
run;

proc sql;
select max(mdy(a.month,1,a.year)) as month2 format MONYY5.
from DEVICE_DAYS as A;
quit;


Ksharp

_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

KSharp-

Thanks for your help, however what I am looking for is the month when all units are entered in the database (10 in total)

so that if October has all 10 entries but November only has seven present, the value returned would be October.

Hope this helps frame the problem.

Thanks again.

Lawrence

Peter_C
Rhodochrosite | Level 12

did you want a monYYYY date in a macro variable for the latest month in which there are 10 rows ?

try

proc sql noprint ;

    select  max(period) format=monyy7.

      into :latest_full_period

      from (

            select mdy( month,1,year)  as period

              from have

          group by month,year

            having count(*) eq 10

           )

;

%put (latest_full_period)=(&latest_full_period) ;

quit;

_LB
Fluorite | Level 6 _LB
Fluorite | Level 6

Peter,

Works for getting the last month-Thanks!

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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