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_date | Inc_by_5 |
| 21955 | 0 |
| 21956 | 0 |
| 21957 | 0 |
| 21958 | 0 |
| 21959 | 0 |
| 21960 | 0 |
| 21961 | 0 |
| 21962 | 0 |
| 21963 | 1 |
| 21964 | 0 |
| 21965 | 0 |
| 21966 | 0 |
| 21967 | 0 |
| 21968 | 1 |
| 21969 | 0 |
| 21970 | 0 |
| 21971 | 0 |
| 21972 | 0 |
| 21973 | 1 |
| 21974 | 0 |
| 21975 | 0 |
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.
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;
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.
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.