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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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 ***/

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18

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 ?

adityaa9z
Obsidian | Level 7

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. 🙂

 

 

Shmuel
Garnet | Level 18

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.

adityaa9z
Obsidian | Level 7
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.

Shmuel
Garnet | Level 18

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 ***/
art297
Opal | Level 21

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

ChrisNZ
Tourmaline | Level 20
@adityaa9z That's when before / after examples save time and confusion.

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