DATA Step, Macro, Functions and more

SAS variable for tracking data step processing

Accepted Solution Solved
Reply
Regular Contributor
Posts: 216
Accepted Solution

SAS variable for tracking data step processing

Hi

I would to determine if there is a record in a data set that contains a specific value for one of the variables in the data set. In other programming languages you can set a global variable (meaning it is available outside of the given function) to equal something when processing records indicates one of the records contains what you are looking for. You can use this variable in other programming steps.

In SAS, is there a way to do this?

Paul


Accepted Solutions
Solution
‎12-05-2013 08:04 AM
Super Contributor
Posts: 282

Re: SAS variable for tracking data step processing

Hi,

Perhaps the %global and %let macro statements would be of help:

SAS(R) 9.2 Macro Language: Reference

SAS(R) 9.2 Macro Language: Reference

E.g.:

%global varvalue;

%let varvalue=123;

data want;

  set have;

  where var=&varvalue;

run;

Regards,

Amir.

View solution in original post


All Replies
Solution
‎12-05-2013 08:04 AM
Super Contributor
Posts: 282

Re: SAS variable for tracking data step processing

Hi,

Perhaps the %global and %let macro statements would be of help:

SAS(R) 9.2 Macro Language: Reference

SAS(R) 9.2 Macro Language: Reference

E.g.:

%global varvalue;

%let varvalue=123;

data want;

  set have;

  where var=&varvalue;

run;

Regards,

Amir.

Super Contributor
Posts: 333

Re: SAS variable for tracking data step processing

I believe %let when used in open code defaults to a global macro variable.

EJ

Super Contributor
Posts: 333

Re: SAS variable for tracking data step processing

Sounds to me like you want to set macro variables but as with a lot of things in SAS there are multiple approaches and Im not sure given the broad nature of the question what would be best in your situation.

In general you can use %let to set a macro variable that basically stores the text you want to use else where in the code:

%let macrovarname = stored value or code ;

then use the macro variable using &macrovarname

Simple example,

%let where = id in (3, 4 , 5, 20 , 33) ;

proc sql;

     create table limitedresults as

     select *

     from olddata

     where &where

     ;

quit;


More specifics about what you are trying to do may help craft a better solution by the community.


EJ   

Regular Contributor
Posts: 216

Re: SAS variable for tracking data step processing

Hi EJ

Your example with a 'macro' variable does not use a macro though. Do you need to use a macro variable only in a marco?

Paul

Super Contributor
Posts: 333

Re: SAS variable for tracking data step processing

Macro variables are available to use outside of a formal macro definition. Try the following code:

%let make = Acura;

data temp;

  set sashelp.cars;

  where make = "&make";

run;

You should get the dataset TEMP with 7 observations.

EJ

Super User
Posts: 5,081

Re: SAS variable for tracking data step processing

As others have indicated, creating a macro variable is the right way to go.  You don't need to define a macro in order to create and use a macro variable.  However ... if you plan to use that macro variable to determine which steps should run later in the program, you will probably need to define a macro.  The reason is that macro language %IF / %THEN statements can appear only inside a macro definition.

Good luck.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 410 views
  • 3 likes
  • 4 in conversation