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

Hi guys,

 

I am trying to create a continuous loop that will meet the following conditions:

- identify first value>= 4;

- if value>=4 then skip the next 2 records (not relevant for my analysis) and starting from record no 3, count next 5 records; after 5 records add a fixed numeric value (3.14);

- if while counting the 5 records in scope you will find another value>=5 then stop counting, add a fixed numeric value (1.03) and start again from the beginning.

 

Could you help me find a solution? Please find below an example of the expected result.

 

Thank you! 

 

VariableNew_column_with_numeric_valueCounterExplanations
1.0 
5.0variable >=5 identified;START
2.0skipp first 2 records 
3.0skipp first 2 records 
1.1start counting
61.030add value for counter<5, reset counter and start againg
1.0skipp first 2 records 
3.0skipp first 2 records 
2.1start counting
1.2 
3.3 
3.4 
13.145counter=5 then 3.14; STOP
1.0wait for next variable>=5
2.0wait for next variable>=5
6.0variable >=5 identified;START
1.0skipp first 2 records 
2.0skipp first 2 records 
3.1start counting
1 2 
1 3 
1 4 
23.145counter=5 then 3.14; STOP
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Vic6 and welcome to the SAS Support Communities!

 

Is the change of criteria from "value>=4" in item 1 and 2 to "value>=5" in item 3 and "variable >=5" in column "Explanations" really part of the rules or just a typo? I assumed the latter and used the threshold 5 consistently. For the test data it doesn't matter anyway (as value 4 doesn't occur).

 

/* Create test data for demonstration */

data have;
input Variable	@@;
cards;
1 5 2 3 1 6 1 3 2 1 3 3 1 1 2 6 1 2 3 1 1 1 2
;

/* Create variables New and Counter */

data want(drop=_:);
set have;
if variable>=5 then
  if _s then do;
    New=1.03;
    _c=0;
  end;
  else _s=1;
if _s then _c+1;
Counter=max(0,_c-3);
if Counter=5 then do;
  New=3.14;
  _s=0;
  _c=0;
end;
retain _s;
run;

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hi @Vic6 and welcome to the SAS Support Communities!

 

Is the change of criteria from "value>=4" in item 1 and 2 to "value>=5" in item 3 and "variable >=5" in column "Explanations" really part of the rules or just a typo? I assumed the latter and used the threshold 5 consistently. For the test data it doesn't matter anyway (as value 4 doesn't occur).

 

/* Create test data for demonstration */

data have;
input Variable	@@;
cards;
1 5 2 3 1 6 1 3 2 1 3 3 1 1 2 6 1 2 3 1 1 1 2
;

/* Create variables New and Counter */

data want(drop=_:);
set have;
if variable>=5 then
  if _s then do;
    New=1.03;
    _c=0;
  end;
  else _s=1;
if _s then _c+1;
Counter=max(0,_c-3);
if Counter=5 then do;
  New=3.14;
  _s=0;
  _c=0;
end;
retain _s;
run;
Vic6
Fluorite | Level 6

Many thanks! It works great!

Vic6
Fluorite | Level 6

One more question. What if I want to skip the first 2 records (from the example above) even if the value =>5? The idea is that the first 2 records are irrelevant regardless the value.

FreelanceReinh
Jade | Level 19

You can extend the first IF-THEN statement as follows to ignore values >=5 in the first two records:

if _n_>2 & variable>=5 then

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 1586 views
  • 2 likes
  • 2 in conversation