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
3
votes
2 answers

How can I call a variable whose name is found as a string under another variable in SAS?

I have one variable that lists the variable name where the information I need is stored. This variable stores all the variable names as strings. I am trying to create another variable that just includes whatever is in the row of the variable whose…
3
votes
2 answers

clearing a library which is being used for stored compiled SAS macro

I have a program which creates a stored compiled macro in a library using the syntax: options mstored sasmstore=MyLib; %macro MyMac() /store source des='My Macro'; %let x=1; %mend; However I cannot seem to re-assign my library (MyLib)…
Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
3
votes
3 answers

SAS - Creating variables from macro variables

I have a SAS dataset which has 20 character variables, all of which are names (e.g. Adam, Bob, Cathy etc..) I would like a dynamic code to create variables called Adam_ref, Bob_ref etc.. which will work even if there a different dataset with…
dblock
  • 33
  • 1
  • 3
3
votes
4 answers

When to use IF or %IF in SAS

I am new to SAS and having a hard time figuring out when should the simple If-Then-else and when should %IF-%THEN-%ELSE should be used. As an example code below: %let inFile = %scan(&sysparm, 1, " "); %macro read_data(infile); data want; infile…
user673218
  • 411
  • 2
  • 8
  • 20
3
votes
1 answer

Parsing header line separately in SAS

I have input file, in which the first line has header information. (Data values which are tab-separated). Amongst these values, there is an integer value that specifies how the rest of the file should be parsed. If this value is less than a…
user673218
  • 411
  • 2
  • 8
  • 20
3
votes
2 answers

SAS macro include guards

In other programming languages such as C++, include guards are used to prevent multiple inclusions of the same code. Like this in C++: #ifndef FOO_INCLUDED #define FOO_INCLUDED .... #endif Does it make sense to build inclusion guards into your SAS…
Martin Bøgelund
  • 1,681
  • 1
  • 17
  • 26
3
votes
2 answers

When I am exporting a SAS dataset to csv; it is trimming all the leading spaces in the characters

When I am exporting a SAS dataset to csv; it is trimming all the leading spaces in the characters. Please help me to retain all the leading spaces in the csv output. The statement used is: Proc Export Data = Globl_Mth_Sumry OutFile =…
Amit Kumar
  • 43
  • 2
  • 7
2
votes
1 answer

How do you assign the result of a macro function to a macro variable in SAS?

I have a macro that creates a timestamp (to append to output file names). However, rather than have to remember what macro variable the macro assigns the value to, I would prefer to assign a macro variable to the result of the macro (if that isn't…
Jay Corbett
  • 28,091
  • 21
  • 57
  • 74
2
votes
1 answer

do loop within a macro

The code below is returning the following error message: Syntax error, expecting one of the following: a name, INPUT, PUT. %macro run_calculation(amt, t, r); data customer_value; i=&r./100.; do n=0 to t; S=&amt.*[(1+ calculated i)^t…
LdM
  • 674
  • 7
  • 23
2
votes
3 answers

Setting table value to macro variable SAS

I need to store a value from a table into a variable. I've tried let, symputx, and select into. In the current version I try to use symputx, but the variable is not being updated. The products table contains type, price_floor, price_tier1,…
2
votes
1 answer

Can you include macro in SAS proc import filename?

I need to import the same excel file into SAS every week, and filenames have different dates like below: file_01012021.xls file_01072021.xls When I set up macro variables I'm getting an error due to the " ' " in the MMDDYYYY macro variable %let…
crlaoy
  • 99
  • 7
2
votes
3 answers

How to concatenate string and numeric SAS Macro and use it in where statement

so I have a code like below %let THIS_YEAR=2020; %macro programall; %do i = 2016 %to &THIS_YEAR; %let num2 =%eval(&i-2000); %let xxx= CAT("MP",&num2); data t_&i.; set table1; where GROUP in ("&xxx"); run; %end; for example when i=2016 num2…
SASPYTHON
  • 1,571
  • 3
  • 14
  • 30
2
votes
1 answer

How to pass a condition as a macro parameter in SAS

In my macro function I need to pass a condition as a parameter , Can i know how to pass a condition like this in sas ex : where flag="YES" %macro counts(con= ,out=); proc sort data=ads(&con.) out=teaes_sev nodupkey; by usubjid surtypen; …
2
votes
2 answers

Do loop within SAS Macro

I am attempting to create a macro that will iterate through the string values of a column. Here is the data: SUBJECT VISIT PARAMETER 001 Baseline param1 001 Visit 2 param1 001 …
statsguyz
  • 419
  • 2
  • 11
  • 35
2
votes
2 answers

SAS: Creating a macro variable containing all months between two dates

I have a macro variable in date yyyymm format, where yyyy is year like 2020 and mm is month like 02, as shown below: %let m=202002; How can I construct a macro variable m_new having as many months in the past as we specify? For eg; if num is 5, we…
cph_sto
  • 7,189
  • 12
  • 42
  • 78