Questions tagged [sas-macro]

A metaprogramming language used in the SAS suite to modify normal SAS code at run-time.

SAS Macro language offers more flexibility for programming SAS code. SAS code written with SAS Macro will be compiled before execution. SAS Macro language uses % for calling SAS macro functions and for calling and defining macros, e.g.:

%macro temp;
  %put %sysfunc(date());
%mend;
%temp;

Variables are used with &, e.g.:

%let my_var = "Hello World";
%put &my_var.;

More about

1265 questions
12
votes
3 answers

Macro returning a value

I created the following macro. Proc power returns table pw_cout containing column Power. The data _null_ step assigns the value in column Power of pw_out to macro variable tpw. I want the macro to return the value of tpw, so that in the main…
hungtx
  • 121
  • 1
  • 1
  • 3
11
votes
5 answers

Is it possible to loop over SAS datasets?

I have 60 sas datasets that contain data on consumers individual characteristics such as id, gender, age, amountSpent, .... Each dataset shows data only for one time period (data1 is Jan, data2 is Feb...). I cannot merge them because of the size…
Buras
  • 3,069
  • 28
  • 79
  • 126
10
votes
2 answers

Why won't my macro variable resolve?

I have a macro variable, &myvar, but it won't resolve when I try to put it in a data step variable. Why won't it, and what can I do to fix this? %let myvar=Hello, world; data _null_; x='&myvar.'; put x=; run;
Joe
  • 62,789
  • 6
  • 49
  • 67
10
votes
1 answer

sas MACRO ampersand

%let test = one; %let one = two; %put &test; %put &&test; %put &&&test; %put &&&&test; %put &&&&&test; Well. I'm TOTALLY BEATEN by these ampersands. I don't understand why they need SO MANY ampersands before a macro variable. Is there any trick to…
zhuoer
  • 617
  • 1
  • 7
  • 15
7
votes
2 answers

Changing Value of Macro Variable inside SAS macro

I am defining a macro variable inside a macro. Then, I am feeding it into a second macro. Inside macro2 counter changes value to 200. However, when I check what is inside the macro variable that I put in after macro 2 runs, it still says 0. I would…
DanRoDuq
  • 260
  • 3
  • 13
7
votes
2 answers

"For in" loop equivalent in SAS 9.3

I'm searching for a while an equivalent of the for in loop (like in Python or in R) in SAS 9.3 macro language. The DO loop seem's to be the solution but did't work exactly as I want. I founded a way to do it in a data step with a DO loop but it…
jomuller
  • 1,032
  • 2
  • 10
  • 19
7
votes
1 answer

Substitute text in a macro variable in SAS

I want to change any instances of a period in a macro variable to underscore. What am I doing wrong? %let pow=0.1; %let x = %sysfunc(tranwrd(&pow,".","_")); %put x=&x; Output: x=0.1
itzy
  • 11,275
  • 15
  • 63
  • 96
7
votes
8 answers

Using SAS Macro to pipe a list of filenames from a Windows directory

I am trying to amend the macro below to accept a macro parameter as the 'location' argument for a dir command. However I cannot get it to resolve correctly due to the nested quotes issue. Using %str(%') does not work, neither do quoting functions…
Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
6
votes
2 answers

SAS macro variable change

In general how do we deal with the situation where macro variables need to be modified inside a macro; for example, suppose I have this macro: %macro test (arg=); array arrayone [&arg]; /* This is ok */ array arraytwo [&arg+1] /* This is not ok.…
francogrex
  • 465
  • 1
  • 4
  • 17
6
votes
4 answers

SAS: Calling one macro from another...Order of Macro Definitions

In my code I have several macros. Macro A is the main macro. Macro A then calls macro B which in turn calls macro C. In SAS, do I have to define them in backwards order? In other words, do I have to define macro C first, then macro B, then…
JT.
  • 85
  • 1
  • 1
  • 8
6
votes
3 answers

In SAS, executing a macro without a semicolon?

I am running a macro multiple times in SAS as follows: %mymac(a,b); %mymac(a,c); . %mymac(a,a) %mymac(a,w); . My program/macro is similar to: /* begin program here */ data original_data; set mylib.mydata; run; %macro mymac(x,y); data…
user27008
  • 600
  • 3
  • 15
  • 24
6
votes
3 answers

Dynamically call macro from sas data step

This code executes fine when Run as a SAS program: %MyMacro(foo_val, bar_val, bat_val); I have created a table using: DATA analyses; input title : $32. weight : $32. response : $32.; datalines; foo1 bar1 bat1 foo2 bar2 bat2 ; I want to…
JustinJDavies
  • 2,663
  • 4
  • 30
  • 52
5
votes
1 answer

submit SAS code or macro from Toolbar

Is it possible to allocate a SAS script or macro to a Toolbar button in Base SAS? ie can you 'dm' a macro or sas script?
Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
5
votes
3 answers

SAS - Break out of macro %DO loop

I know there exists the LEAVE statement for data step DO loops to terminate the current loop. I cannot find, however, documentation for a corresponding macro command. I've tried %LEAVE but that appears not to be defined. Does SAS not have a…
Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
5
votes
1 answer

macro variable is uninitialized after %let statement in sas

I want to create something in SAS that works like an Excel lookup function. Basically, I set the values for macro variables var1, var2, ... and I want to find their index number according to the ref table. But I get the following messages in the…
Yimai
  • 87
  • 6
1
2 3
84 85