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

Hi all,

 

First time user and still a newbie at SAS. I'm working on just basic descriptives on a dataset that contains 50+ variables with  age in months (ageinmonths) as a variable ranging from 1 months to 120 months. The outcome variable contains two measurements: one is the age in months (ageinmonths_test) when a secondary questionnaire was administered, while another is a parallel of this but states the actual date (date_test) in YYMMDD10 format (ex. 2012-01-22 or January 22, 2012). 

 

I wanted to know how to I can produce different datasets by restricting to only those ex: 

1) between 10 months to 50 months

2) between 10 months to AT LEAST 8 months prior to their secondary questionnaire testing date

3) between 10 months to AT LEAST 4 months prior to their secondary questionnaire testing date

 

I've managed to do the first one through the code below:

 

data test1;
set original;
if ageinmonths <10 then delete;
if ageinmonths >50 then delete;
run;

However, I'm having trouble figuring out the 2nd and 3rd bits (most likely due to my lack of SAS knowledge). I've tried to do something similar as the code above but it didn't seem to work:

 

 

data test2;
set original;
if ageinmonths <12 AND ageinmonths_test <8 then keep;
else delete;
run;

 

In any case, any help would be much appreciated! Thanks in advance! 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

If date_test is stored as a SAS date, then you can use the INTNX function to specify a date that is 8 months (or 4 months) prior.

See the article "INTCK and INTNX: Two essential functions for computing intervals between dates in SAS"

Here is an example that shows how to count back 8 months:

 

data Y;
format Date_test Prior8 Date10.;
date_test = '08Aug2020'd;
Prior8 = intnx('month', date_test, -8, 'same');
output;
date_test = '31Oct2020'd;
Prior8 = intnx('month', date_test, -8, 'same');
output;
run;

proc print;run;

Because some months have 28/29 or 30 days whereas others have 31, the "8 months prior" will adjust the date in certain circumstances. In the second example, I ask for 8 months prior to 31Oct. Because 8 month prior is February and because February never has 31 days, the result will be the last day in February (28 or 29, accounting for leap years).

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

If date_test is stored as a SAS date, then you can use the INTNX function to specify a date that is 8 months (or 4 months) prior.

See the article "INTCK and INTNX: Two essential functions for computing intervals between dates in SAS"

Here is an example that shows how to count back 8 months:

 

data Y;
format Date_test Prior8 Date10.;
date_test = '08Aug2020'd;
Prior8 = intnx('month', date_test, -8, 'same');
output;
date_test = '31Oct2020'd;
Prior8 = intnx('month', date_test, -8, 'same');
output;
run;

proc print;run;

Because some months have 28/29 or 30 days whereas others have 31, the "8 months prior" will adjust the date in certain circumstances. In the second example, I ask for 8 months prior to 31Oct. Because 8 month prior is February and because February never has 31 days, the result will be the last day in February (28 or 29, accounting for leap years).

mkeintz
PROC Star

The KEEP statement is not an executable statement - it tells SAS what variables to include in the output data set.  And because it is the sas compiler (not the executor) that honors your KEEP statement (see below), it is a standalone statement - it can't be the result of an IF test.

 

data want;
  set sashelp.class;
  keep  name sex age;
run;

In other words, KEEP is not the opposite of DELETE.  You want the OUTPUT statement.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 2 replies
  • 756 views
  • 2 likes
  • 3 in conversation