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

Hi Sas Users,

 

I am after some assistance to help with a problem I have with incrementing after a certain condition.

 

This is my data. 

 

Sas_Date = is a sas_date in numeric

Inc_by_5 = an integer.

 

Have a list of SAS dates. I want to flag every observation that is 5 more than the condition.

 

The condition is to start at date 21963 and then Add 5,10,15...etc until EOF to this date and put a "1" in Inc_by_5 variable.

 

 

Have:

 

sas_date
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974

21975

etc...

 

Want:

 

sas_dateInc_by_5
219550
219560
219570
219580
219590
219600
219610
219620
219631
219640
219650
219660
219670
219681
219690
219700
219710
219720
219731
219740
219750

etc...

 

 

Code so far.... but I think I need a macro....?

 

 

%let j=0;
data Want;
set Have;

if date = 21963 + (5*&j) then do;
Inc_by_5=1;
end;
else do;
Inc_by_5=0;
%let j = %eval(&j+1);
end;

run;

 

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input sas_date;
cards;
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
;
%let date=21963;
data want;
 set have;
 retain flag 0 n -1;
 if sas_date=&date then flag=1;
 if flag then n+1;
 Inc_by_5=ifn(mod(n,5)=0,1,0) ;
drop n flag;
run;

View solution in original post

3 REPLIES 3
Satish_Parida
Lapis Lazuli | Level 10

This code is wrong, you can not do macro variable manipulation inside a data step do loop using %let.

The example you provided is not clear.

Are you asking to mark the flag variable Inc_by_5 to 1 when date =21963 + (a multiple of 5)?

 

If that is the case this is the code

 

data have;
input sas_date;
cards;
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
;
run;

data want;
set have;
if sas_date-21963 ge 0 and  mod((sas_date-21963),5)=0 then Inc_by_5=1;
else Inc_by_5=0;
run;

Please let us know if this worked for you.

andreas_lds
Jade | Level 19

I fully agree to @Satish_Parida : "The example you provided is not clear." And, of course, you don't need macro-variables, just use retain-statement to prevent j from being reset.

Untested:

data Want;
  set Have;
  
  retain j 0;

  if date = 21963 + (5*j) then do;
    Inc_by_5=1;
  end;
  else do;
   Inc_by_5=0;
   j = j + 1;
  end;
run;
Ksharp
Super User
data have;
input sas_date;
cards;
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
;
%let date=21963;
data want;
 set have;
 retain flag 0 n -1;
 if sas_date=&date then flag=1;
 if flag then n+1;
 Inc_by_5=ifn(mod(n,5)=0,1,0) ;
drop n flag;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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