Programming the statistical procedures from SAS

Setting a variable value outside a PROC

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 75
Accepted Solution

Setting a variable value outside a PROC

Hi,

This code is wrong and does not work:

myname= 'SMITH';

DATA lookup;                          

    SET some_data_file;

    KEEP lname and some other variables;

    WHERE lname = myname;

RUN;

but it shows what I want to do.

I will have multiple reads data grabs and reads and rather than change the value for a variable (in this case lname) in multiple places (even though find and replace is nice!) I'd like to set it once globally outside the scope of the DATA statements (or even PROC statements) and then be able to use it later in those statements.

How is this done??


Accepted Solutions
Solution
‎04-10-2014 01:14 PM
Super Contributor
Posts: 1,636

Re: Setting a variable value outside a PROC

%let myname= "SMITH";

DATA lookup;                          

    SET some_data_file;

  KEEP lname and some other variables;

    WHERE lname =&myname;

RUN;

View solution in original post


All Replies
Grand Advisor
Posts: 16,875

Re: Setting a variable value outside a PROC

Macro variables.

I've moved the quotes, but where you include them is up to you.

%let myname = SMITH;

DATA lookup;                          

    SET some_data_file;

    KEEP lname and some other variables;

    WHERE lname = "&myname";

RUN;

Solution
‎04-10-2014 01:14 PM
Super Contributor
Posts: 1,636

Re: Setting a variable value outside a PROC

%let myname= "SMITH";

DATA lookup;                          

    SET some_data_file;

  KEEP lname and some other variables;

    WHERE lname =&myname;

RUN;

Frequent Contributor
Frequent Contributor
Posts: 75

Re: Setting a variable value outside a PROC

Outstanding!

Respected Advisor
Posts: 4,756

Re: Setting a variable value outside a PROC

On another note, since you plan on running this multiple times you can speed it up by moving KEEP:

data lookup;

   set some_data_file (keep=lname and some other variables);

   where lname= ...

run;

This will limit which variables get read in.  The original program reads in all the variables, and then limits what gets output.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 206 views
  • 6 likes
  • 4 in conversation