Hi ,
Is there a way the below hardcoding be replaced by loops or arrays? lower bound is 1986 and upper can be 2026
if 1976<= Manuf_Year < 1985 then TestYr="1976-1984";
else if 1985<=Manuf_Year <1995 then TestYr="1985-1994";
else if 1995 <= Manuf_Year LT 2005 then TestYr="1995-2004";
Use a do loop to build a control dataset for proc format; take care of the border cases in the loop.
After that, it is a simple put() or format assignment:
data cntlin;
fmtname = "myfmt";
type = 'N';
do start = 1975 to 2025 by 10;
end = start + 9;
if start = 1976 then start = 1976;
if end = 2034 then end = 2026;
label = catx('-',start,end);
output;
end;
run;
proc format cntlin=cntlin;
run;
data want;
input year;
format year myfmt.;
testyear = put(year,myfmt.);
cards;
1976
2010
2026
2020
;
run;
@SASPhile wrote:
Hi ,
Is there a way the below hardcoding be replaced by loops or arrays? lower bound is 1986 and upper can be 2026
if 1976<= Manuf_Year < 1985 then TestYr="1976-1984";
else if 1985<=Manuf_Year <1995 then TestYr="1985-1994";
else if 1995 <= Manuf_Year LT 2005 then TestYr="1995-2004";
You state "lower bound is 1986" and then do not display anything about the "upper bound", which is sort of implied by the example code. Do intend 10 year intervals such as 1986-1995?
If so this may get you started.
data work.cntlin; fmtname='tenyear'; type='N'; do start=1986 to 2026 by 10; end= start+9; label = catx('-',start,end); output; end; run; proc format library=work cntlin=work.cntlin; run; data example; input year; y = put(year,tenyear.); datalines; 1988 1999 2001 2016 2030 ; run;
You would have to ensure the format was available for use by either placing the format in a library on the FMTSEARCH path or rerunning it the format code. I suggest making sure the code doesn't get lost.
Most of the time when you are wanting to display a value based on a single variable then a custom format may be appropriate.
You need the MAX() because you have an upper or lower bound, otherwise this would do groups from YYY5 to YYY5+9
period = catx("-", max(round(year, 10)-5, 1976), max(round(year, 10)+4, 2026));
EDIT this should probably be MIN instead of MAX at last argument.
period = catx("-", max(round(year, 10)-5, 1976), min(round(year, 10)+4, 2026));
@Reeza: Nice compact solution, where I'm sure you meant min(...) in the third argument of CATX.
@FreelanceReinh Yup, that would help 🙂
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.