SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
JJP1
Pyrite | Level 9
Hi. I have a dataset contains year, month columns but I don't have day columns. So using this columns I need to write a condition to filter only 12months old records from today Please help. Year is holding as 2019 and month is 10
10 REPLIES 10
PaigeMiller
Diamond | Level 26

Set the day to be 1 in the MDY function.

--
Paige Miller
JJP1
Pyrite | Level 9
Thanks @Paige. Would you please help on complete code please
PaigeMiller
Diamond | Level 26

@JJP1 wrote:
Thanks @Paige. Would you please help on complete code please

What is wrong with the code from @unison ?

--
Paige Miller
unison
Lapis Lazuli | Level 10

Here's an example. This dataset contains dates in the 1990's so I set today to 12/31/1994 but you can use:

 

%let tdy = %sysfunc(today(),date9.);
%put =&tdy;

 

Example:

 

%let tdy = '31DEC1994'd;

data have;
set sashelp.prdsale;
format date date9.; 
month_num = month(month);
date = mdy(month_num,1,year);
run;

data want;
set have;
where date between intnx("month",&tdy.,-11,"b") and &tdy.;
run;

proc freq data=want; 
tables month*year /nocol nocum nopercent norow; 
run;

-unison

-unison
JJP1
Pyrite | Level 9
Why at the end proc freq you are using please
unison
Lapis Lazuli | Level 10

Just as a check to make sure the correct observations are pulled.

 

-unison

-unison
Astounding
PROC Star

You have to do the hard part ... define the problem better.  

 

Today is November 22, 2019.  Using only month and year, what month/year combinations would be included in your 12 month period?

 

Once the rules are established, the programming would be relatively easy for many of the people who post here.

JJP1
Pyrite | Level 9
Sorry for confusion.
I have below sample data
Number year month
19 2019 10
20 2019 10
30 2018. 5

I want to keep the above data only for 12 months older .i can do this only
using year and month columns.
so sample output should exclude 2018 record as it is not 12 months older .
I know I can use mdy function.but Iam not sure how to apply the function.
Please help




Astounding
PROC Star

The key issue is still unaddressed.  Let me spell it out in a more specific way.

 

The following represent 3 dates on which the program might run:

 

November 30, 2019

December 1, 2019

December 15, 2019

 

For each date, specify the first year/month and the last year/month you would like to include.

Pmyosh
Obsidian | Level 7

Hi JJP1,

 

Do you mean all your data looks like this:

Year Month

2019 01

2018 02

2019 03

 

But you dont have a day Variable? Is your intention to get the full 12 months regardless of the date?

E.g., today is Nov 25, 2019,  you want to include From Dec 01, 2018 to today?

Since you don't have day variable, you can default to 01 all the days.

 

Then read as yyyymmdd, then use intck to count 12 months from today

 

data ck ;
year=2019 ;
month=9 ;
new_month = COMPRESS(PUT(year,4.)||PUT(month,z2.)||"01") ;

FORMAT inputasdate date9. ;
inputasdate = INPUT(new_month,yymmdd8.) ;
_12months = intck("months",inputasdate, today()) ;

IF 0<= _12months <=12 ;
RUN ;

 

Please take note that the year and month, I suppose they are numbers

Please let us know if we get your issue correctly?

 

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2868 views
  • 2 likes
  • 5 in conversation