Dear all,
I create multiple data sets from one original dataset by following codes,
DATA step1.year1991
step1.year1992
step1.year1993
step1.year1994
step1.year1995
step1.year1996
step1.year1997
step1.year1998
step1.year1999
step1.year2000
step1.year2001
step1.year2002
step1.year2003
step1.year2004
step1.year2005
step1.year2006;
SET old-data-set;
IF earliest_filing_date = 1991 THAN output step1.year1991;
ELSE IF earliest_filing_date = 1992 THAN output step1.year1992;
ELSE IF earliest_filing_date = 1993 THAN output step1.year1993;
ELSE IF earliest_filing_date = 1994 THAN output step1.year1994;
ELSE IF earliest_filing_date = 1995 THAN output step1.year1995;
ELSE IF earliest_filing_date = 1996 THAN output step1.year1996;
ELSE IF earliest_filing_date = 1997 THAN output step1.year1997;
ELSE IF earliest_filing_date = 1998 THAN output step1.year1998;
ELSE IF earliest_filing_date = 1999 THAN output step1.year1999;
ELSE IF earliest_filing_date = 2000 THAN output step1.year2000;
ELSE IF earliest_filing_date = 2001 THAN output step1.year2001;
ELSE IF earliest_filing_date = 2002 THAN output step1.year2002;
ELSE IF earliest_filing_date = 2003 THAN output step1.year2003;
ELSE IF earliest_filing_date = 2004 THAN output step1.year2004;
ELSE IF earliest_filing_date = 2005 THAN output step1.year2005;
ELSE IF earliest_filing_date = 2006 THAN output step1.year2006;
RUN;
Could you please give me some suggestion to improve these codes if I do not want to copy the similar codes so many times?
many thanks in advance,
best regards,
France
As Reeza mentioned, in all likelihood there isn't a good reason to do this. But if you really must, you can use macro language. It won't shorten the code, but it will shorten the tedium of having to type it out. For example:
%macro multiple_years;
%local k;
data
%do k=1991 %to 2006;
step1.year&k
%end;
;
set old_data_set;
if earliest_filing_date=1991 then output step1.year1991;
%do k=1992 %to 2006;
else if earliest_filing_date=&k then output step1.year&k;
%end;
run;
%mend multiple_years;
%multiple_years
There really isn't a good reason to do this, it usually only needed when you'll run out of memory or because you're absolutely required to analyze it.
Since people rarely listen to this advice:
https://blogs.sas.com/content/sasdummy/2015/01/26/how-to-split-one-data-set-into-many/
@France wrote:
Dear all,
I create multiple data sets from one original dataset by following codes,
DATA step1.year1991 step1.year1992 step1.year1993 step1.year1994 step1.year1995 step1.year1996 step1.year1997 step1.year1998 step1.year1999 step1.year2000 step1.year2001 step1.year2002 step1.year2003 step1.year2004 step1.year2005 step1.year2006; SET old-data-set; IF earliest_filing_date = 1991 THAN output step1.year1991; ELSE IF earliest_filing_date = 1992 THAN output step1.year1992; ELSE IF earliest_filing_date = 1993 THAN output step1.year1993; ELSE IF earliest_filing_date = 1994 THAN output step1.year1994; ELSE IF earliest_filing_date = 1995 THAN output step1.year1995; ELSE IF earliest_filing_date = 1996 THAN output step1.year1996; ELSE IF earliest_filing_date = 1997 THAN output step1.year1997; ELSE IF earliest_filing_date = 1998 THAN output step1.year1998; ELSE IF earliest_filing_date = 1999 THAN output step1.year1999; ELSE IF earliest_filing_date = 2000 THAN output step1.year2000; ELSE IF earliest_filing_date = 2001 THAN output step1.year2001; ELSE IF earliest_filing_date = 2002 THAN output step1.year2002; ELSE IF earliest_filing_date = 2003 THAN output step1.year2003; ELSE IF earliest_filing_date = 2004 THAN output step1.year2004; ELSE IF earliest_filing_date = 2005 THAN output step1.year2005; ELSE IF earliest_filing_date = 2006 THAN output step1.year2006; RUN;
Could you please give me some suggestion to improve these codes if I do not want to copy the similar codes so many times?
many thanks in advance,
best regards,
France
As Reeza mentioned, in all likelihood there isn't a good reason to do this. But if you really must, you can use macro language. It won't shorten the code, but it will shorten the tedium of having to type it out. For example:
%macro multiple_years;
%local k;
data
%do k=1991 %to 2006;
step1.year&k
%end;
;
set old_data_set;
if earliest_filing_date=1991 then output step1.year1991;
%do k=1992 %to 2006;
else if earliest_filing_date=&k then output step1.year&k;
%end;
run;
%mend multiple_years;
%multiple_years
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.