BookmarkSubscribeRSS Feed
Solph
Pyrite | Level 9

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;

9 REPLIES 9
art297
Opal | Level 21

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

Reeza
Super User

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;

Solph
Pyrite | Level 9

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.

art297
Opal | Level 21

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

dsname was just a variable that Reeza created.

Solph
Pyrite | Level 9

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,

Reeza
Super User

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 var:);

run;

ArtC
Rhodochrosite | Level 12

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

total=sum(of f2000-f2011);

Reeza
Super User

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.

Solph
Pyrite | Level 9

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 1071 views
  • 0 likes
  • 4 in conversation