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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

View solution in original post

3 REPLIES 3
Reeza
Super User

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


 

France
Quartz | Level 8
Dear Reeza,
many thanks for your help.
Astounding
PROC Star

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2073 views
  • 0 likes
  • 3 in conversation