DATA Step, Macro, Functions and more

Assigning parameter values.

Reply
N/A
Posts: 0

Assigning parameter values.

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.

How can that be made with SAS code?

/Anne
N/A
Posts: 0

Re: Assigning parameter values.

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 &macrovariable. So for example:

At the top of the program:

%let year=1995;

And in your datastep:

data output;
set input;
where year=&year.;
run;

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.

Cheers
Peter
N/A
Posts: 0

Re: Assigning parameter values.

So it's necessary to change code each year, although only one line.

There's nothing like an input box that you can launch with SAS code and pass the year value with?

/Anne
N/A
Posts: 0

Re: Assigning parameter values.

Hi Anne,

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.

Cheers
Peter Message was edited by: pznew
N/A
Posts: 0

Re: Assigning parameter values.

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.
N/A
Posts: 0

Re: Assigning parameter values.

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;
%global year;
*a macro to get the user input;
%macro get_year();
*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;
%display get_year;
%mend get_year;

*call the macro;
%get_year;

*use the macro vairable created;
data output_dataset;
set input_dataset;
where year=&year;
run;

Even if this is not what you were after it has provided me with a little more knowledge. Thanks.
Super Contributor
Posts: 359

Re: Assigning parameter values.

Another method is to create an external file, (spreadsheet or text), containing the years to run. Read the file in and loop over observations in the resulting dataset running analysis for each one.

There are many ways to accomplish this in SAS. You just need to find the one that fits the environment you are working in.
Contributor
Posts: 36

Re: Assigning parameter values.

Anne: You can create a data set at the start of the program that will create a macro variable that lists the year for 2 years ago.

data _null_;
analysis_date=today();
previous2_year_date=intnx('year',analysis_date,-2);
call symputx('year_start',year(previous2_year_date));
run;

If this were run today:
analysis_date=March 19, 2009
previous2_year_date = January 1, 2007
macro variable &year_start resolves to 2007
The macro variable will automatically change every year.
Ask a Question
Discussion stats
  • 7 replies
  • 198 views
  • 0 likes
  • 3 in conversation