I have hundred thousands of records in my data table with data from 1990 an onwards.
After each year I want to run my code and get detailed statistics for that year, and some summary statistics for the 2 years before that year. I have code that produces these statistics. In that code I assign the year value to a variable in a data step.
But I would like to pass the year value when the program starts running. I don't want to be forced to change code text each new year.
In any "ordinary" programming language that would be easy.
SAS macro language. Quite a large subject to cover on a forum post but very basically, to get what you seem to want you would put a %let statement for your parameter and replace the hard coded year value with an ¯ovariable. So for example:
At the top of the program:
And in your datastep:
If your year value in your data is character then surround the resolution with double quotes e.g. where year="&year."; I could go on but macro language is a whole new subject to learn. These are the basics and hopefully gives you what you are looking for.
You could make the program dynamic. If the program runs the data for the year previous to the current year, you can code that so that SAS can figure out what the year was by using the system date. I'm not sure that is what you are after though.
There are ways to get user input from a window using the %window statement but it's pretty complicated. Feel free to look it up if you think it could save you time.
When you invoke SAS, you can enter SYSPARM as an OPTION e.g. SYSPARM=’1995’. (The precise details how to do this will depend on your platform.) This is then available as a macro variable &SYSPARM.
In my own code, I have a macro to analyse SYSPARM because a complex program may have several parameters to check, e.g. SYSPARM=’YEAR=1995'. However, this involves functions like %INDEX and %SUBSTR, which would not be required for basic code.
OK, so you got me going on one of those little tours of the internet. I looke dinto %window and I think you need.
*create the macro variable as a global;
*a macro to get the user input;
*define the window;
%window get_year columns=90 rows=15 irow=1 icolumn=1
#2 @5 'Please enter the year required:' @45 year 4;
*bring up the window and get input;
*call the macro;
*use the macro vairable created;
Even if this is not what you were after it has provided me with a little more knowledge. Thanks.