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;
%do is only valid if it is part of a SAS macro that you create.
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;
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.
http://support.sas.com/kb/34/513.html
dsname was just a variable that Reeza created.
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,
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;
The OF operator can also be used to eliminate the need for the commas.
total=sum(of f2000-f2011);
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.
Thanks for all the tips and info. It's such a great community for help. I really appreciate it.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.