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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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