Help using Base SAS procedures

%do year %to year in SET statement

Reply
Frequent Contributor
Posts: 107

%do year %to year in SET statement

Hi

I just have a very simple question for a code I used before but just wouldn't work now. Basically I just wanted to use %do year %to year to stack up a series of data sets from multiple years. The example I'm using below is just for 3 years. For some reasons the following code isn't working

     data combined;

         set %do year = 2009 %to 2011;

             d&year

         %end;;

     run;

and would give me error messages "%DO statement is not valid in open code" and pointed to d&year as

- Syntax error, expecting one of the following: a name, a quoted string, (, -, :,;, END, INDSNAME, KEY, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

- The symbol is not recognized and will be ignored.

Same data are below. Thanks a great deal.

data d2009; input

id;

datalines;

1

2

4

;

data d2010; input

id;

datalines;

3

5

;

data d2011; input

id;

datalines;

3

6

;

data combined;

    set %do year = 2009 %to 2011;

            d&year

    %end;;

run;

PROC Star
Posts: 7,467

Re: %do year %to year in SET statement

%do is only valid if it is part of a SAS macro that you create.

Super User
Posts: 19,770

Re: %do year %to year in SET statement

Well the log is right, you can't do that in open code, that's macro code, so you can wrap all of your code in macro or use something different.

Ie

data combined;

    set d2009-d2011 indsname=temp;

    dsname=temp;

run;

OR

data combined2;

    set d20: indsname=temp;

    dsname=temp;

run;

Frequent Contributor
Posts: 107

Re: %do year %to year in SET statement

Thanks Arthur. It's really stupid of me totally forgetting about it.

Reeza, what do indsname and dsname do? Any hint is appreciated and I'll then google to find more about it.

Thanks.

PROC Star
Posts: 7,467

Re: %do year %to year in SET statement

http://support.sas.com/kb/34/513.html

dsname was just a variable that Reeza created.

Frequent Contributor
Posts: 107

Re: %do year %to year in SET statement

Thanks Arthur.

While I'm on it, I've another related, but not so related question. I want to create a variable based on the sum of a series of variable ending in numerical order. Is there any shortcut to do it. (I can't use f2000-f2011 since I need comma in between variables to do sum or average,).

total=sum(f2000,f2001,f2002,f2003,f2004,f2005,f2006,f2007,f2008,f2009,f2010,f2011);

Thanks,

Super User
Posts: 19,770

Re: %do year %to year in SET statement

You could create an array or use the colon operator if your variables are name f20XX consistently.

Double check to make sure its giving you what you wanted.

data have;

    array test(10) var1-var10;

    do i=1 to 10;

    test(i)=5;

    end;

    total=sum(of test(*));

    total2=sum(of varSmiley Happy;

run;

Valued Guide
Posts: 634

Re: %do year %to year in SET statement

The OF operator can also be used to eliminate the need for the commas.

total=sum(of f2000-f2011);

Super User
Posts: 19,770

Re: %do year %to year in SET statement

indsname is a SAS option that stores the name of the source dataset when stacking multiples together that I remember by IN DataSet NAME


And dsname is just a variable created to store the value.

Frequent Contributor
Posts: 107

Re: %do year %to year in SET statement

Thanks for all the tips and info. It's such a great community for help. I really appreciate it.

Ask a Question
Discussion stats
  • 9 replies
  • 317 views
  • 0 likes
  • 4 in conversation