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
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.
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.
I believe %let when used in open code defaults to a global macro variable.
EJ
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 ¯ovarname
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
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
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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.