BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Paul_NYS
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

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

6 REPLIES 6
Amir
PROC Star

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.

esjackso
Quartz | Level 8

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

EJ

esjackso
Quartz | Level 8

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   

Paul_NYS
Obsidian | Level 7

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

esjackso
Quartz | Level 8

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

Astounding
PROC Star

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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