Solved
Contributor
Posts: 30

# SAS Logic question

Hello Experts,

Need a suggestion ,regarding one of the programming logics I need to figure out. There is a dataset named A and in A there is variable named rex, which has values Yes or No and a variable named ram which has a value of r or Null.

1) If in the last THREE days, the value of rex is Yes, then we have to check the following conditions.

--Check whether the value of rex was "Yes" in the past SIX days AND value of ram was not equal to "r", - If this condition matches, then the value of ram should be populated as "r", else Null.

2) If not the value of ram stays as null.

The jobs runs daily for each day, so the data will check the previous days to match the conditions and populate the values of ram based on it.

Please let me know if this makes some sense or if you have more questions around it.

Accepted Solutions
Solution
‎07-20-2017 01:25 PM
Posts: 1,848

## Re: SAS Logic question

Assuming you have or can calculate the observation date:

``````data _null_;
set have;
td = today();
d_3 = td - 3;
d_6 = td - 6;
call symput('firstobs', left(obs - 10));
call symput('d_3', left(d_3));
call symput('d_6', left(d_6));
run;

data want;
set have end = eof;
retain stat_rex stat_ram 0;
if date  > &d_3 and
rex = 'YES'    then stat_rex = 1;
if  date > &d_6 and ram ne  'r' then  stat_ram = 1;

if eof and stat_rex=1 and stat_ram = 1 then ram = 'r' ;
else ram = ' ';     /* last obs is todays obs, last appended */       drop stat_rex stat_ram;
run;/*** I cannot test it so check it carefully ***/``````

All Replies
Posts: 1,848

## Re: SAS Logic question

1) Is the date part of dataset name or is it a variable in the dataset?

2) What is the name-format of the datasets? how do you distinguish between datasets of different days?

3) please post at least on dataset example, better in a format like:

``````data test;
input rex \$ ram \$;
cards;
a r
a
;
run;``````

4) Does each daily dataset have one row only or more?

If more - which row should be checked ?

Contributor
Posts: 30

## Re: SAS Logic question

1) Date is not the part of dataset. I am calculating it from today -3 days and today - 6 days.

2) Its just a single dataset. Records get appended to it daily. So if today is 1/1/2017. Records for 12/31/2016 will get appended to A.

4) Its a single dataset, has a million records and records get appended to i daily..

Also, take for example
Below are the records which already exist in the DS

date rex ram
12/26 Yes .
12/27 Yes r     /*(Has rex as yes in past 3 days and ram <> null in last 6 days, so ram is populated as r)*/

12/28 No .
12/29 Yes r     /*(Has rex as yes in past 3(12/27) days and ram <> null(12/26) in last 6 days, so ram is populated as r)*/
12/30 No  .
12/31 No .
1/1 No .
1/2 Yes .   /*(rex is no in past 3 days so ram is null)*/

Let me know if this helps.

Posts: 1,848

## Re: SAS Logic question

Am I right:

You have a dataset with 3 variables: date, rex, ram.

The dataset contains one observation per day, accumulating more than one year.

The date is in a format of mm/dd without a year info.

Daily data is appended to the end of the dataset.

Then you are intertested in the last 6 observations only, in order to check according to rules and assign a value to ram variable.

I need stop now and continue later.

Contributor
Posts: 30

## Re: SAS Logic question

Right. Year info is available too. I just didnt include it here since it was an example.

I am interested in checking a value of "Yes" for rex in last 3 days and if that happens..

A value of ram <> r in the past 6 days.

if these both conditions match.. the value for ram today will be r.

Solution
‎07-20-2017 01:25 PM
Posts: 1,848

## Re: SAS Logic question

Assuming you have or can calculate the observation date:

``````data _null_;
set have;
td = today();
d_3 = td - 3;
d_6 = td - 6;
call symput('firstobs', left(obs - 10));
call symput('d_3', left(d_3));
call symput('d_6', left(d_6));
run;

data want;
set have end = eof;
retain stat_rex stat_ram 0;
if date  > &d_3 and
rex = 'YES'    then stat_rex = 1;
if  date > &d_6 and ram ne  'r' then  stat_ram = 1;

if eof and stat_rex=1 and stat_ram = 1 then ram = 'r' ;
else ram = ' ';     /* last obs is todays obs, last appended */       drop stat_rex stat_ram;
run;/*** I cannot test it so check it carefully ***/``````
Super User
Posts: 8,218

## Re: SAS Logic question

In addition to the questions @Shmuel asked, here are a couple more:

1. are you just adding a new record for the current day or is that record already in the dataset

2. is it just one record or is there also some kind of id field and this has to be done for all ids

3. if the code has to add a record for today, how should the variable ram be populated (i.e., yes or no)

Art, CEO, AnalystFinder.com

Super User
Posts: 2,512