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

How can I see the original sas macro code?

I'm trying to debug code used in a SAS program, but the program uses macros from a permanent library. I can't find the code that created the macros and I don't have access to the person who created the original library. I know that option mprint;…
Sam Dickson
  • 5,082
  • 1
  • 27
  • 45
4
votes
1 answer

SAS - check if external file (.csv) is empty; if not, import into SAS

I have several .csv files in a folder that I would like to import into SAS. However, they are not always populated with data, so when I attempt to import an empty file into SAS, I get an error. I am wondering if there is some way for me to check…
user3023153
  • 47
  • 1
  • 5
4
votes
2 answers

Resolving multiple ampersands with macro variables

This code works in SAS EG run on local (hidden sensitive information): *---- two values: DEV (ALIASDEV) and PROD (ALIASPROD); %let my_environment = ALIASDEV; %let ALIASPROD= (hidden_tns_prod); %let ALIASDEV= (hidden_tns_dev); libname mylib oracle…
Stephane Maarek
  • 5,202
  • 9
  • 46
  • 87
4
votes
3 answers

Executing SAS Code from string in dataset

Is it possible to read (and execute) SAS code that is stored in a string in a SAS Dataset. For example, the dataset 'CODE' contains a string variable, which contains "IF TOTAL_SALES GE 20000 AND TYPE IN ('A', 'B', 'C') THEN VAR1 = 'Y' ;" Can I do…
Tim
  • 41
  • 1
  • 2
4
votes
2 answers

Mass Convert Multiple SAS datasets into CSV files

I have around 720 SAS datasets (which are equipped with 720 SAS index files ".sas7bdnx") within a single folder that I would like to convert to CSV files. I know how to use proc export to convert one at a time, but any efficient way to do the…
Alex Chen
  • 535
  • 1
  • 5
  • 7
4
votes
1 answer

How to create new macro variable from existing macro variables using calculations in SAS?

I'd like to create a new macro variable from other macro variables that already exist. I have tried multiple variations of call symput, %eval , and input to no avail... I would like d to evaluate to 3 / 30 = .10. ***** taken directly from the sas…
blue and grey
  • 393
  • 7
  • 21
3
votes
1 answer

"Save As" and "Close file" without using macro

I usually have a sas macro code which automatically run macros "Save As" and "Close file" in the excel spreadsheet on running the sas code and after populating the data into the excel file. The problem I have is that the excel file I have right now…
Nupur
  • 347
  • 8
  • 19
  • 36
3
votes
3 answers

SAS MACRO to Cycle Month End Dates

I have a set of code that I am manually adjusting month end dates. The query which runs and uses the dates has left joins to pull data for each declared date. I've been looking at macros because this is labor intensive to change all the dates by one…
Mark
  • 327
  • 1
  • 7
  • 14
3
votes
2 answers

How to make a SAS macro run through variables/columns?

I am trying my hand at SAS macros for the first time. My basic question is: I have a dataset with some 10000 variables. I need to take each column individually, create a new conditional variable, store the results, then move to the next column.…
Samantha
  • 45
  • 2
  • 6
3
votes
1 answer

Make the output of proc tabulate to vertical?

My SAS code is as follow: DATA CLASS; INPUT NAME $ SEX $ AGE HEIGHT WEIGHT; CARDS; ALFRED M 14 69.0 112.5 ALICE F 13 56.5 84.0 BARBARA F 13 65.3 98.0 CAROL F 14 62.8 102.5 HENRY M 14 63.5 102.5 RUN; PROC PRINT; TITLE…
Xia.Song
  • 416
  • 3
  • 15
3
votes
3 answers

SAS Macro GLOBAL scope

is there a short way to make ALL macro variables created inside a macro global in scope? ie: %macro x; %global _all_; * ??? ; %let x=1; %let y=1; %let z=1; %mend;
Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
3
votes
1 answer

SAS: how to know which Macro called current Macro?

Imagine that a certain macro M1 was called by a different macro and is being executed. Is there a way to get access to the name of the macro that called M1 in that specific instance without the calling macro having been explicitly programmed to pass…
Marco
  • 313
  • 1
  • 2
  • 11
3
votes
4 answers

SAS does not evaluate the content of %PUT

I use this bit outside of any DATA step. %let sth = 20191111; %let sthelse=SUBSTR(INPUT(&sth.,12.),1,4); %put &sthelse.; It does not yield '2019', which I would expect but rather SUBSTR(INPUT(20191111,12.),1,4) What goes wrong here?
E. Sommer
  • 710
  • 1
  • 7
  • 28
3
votes
3 answers

Is there a way to skip missing data sets when iterating through names?

I have a few data sets in SAS which I am trying to collate into one larger set which I will be filtering later. They're all called something like table_201802. My problem is that there are a few missing months (i.e. there exists table201802 and…
davkav9
  • 137
  • 1
  • 1
  • 6
3
votes
1 answer

SAS adding a prefix to all words in a macro variable

I am adding a prefix to each word in a macro variable. However, when using my current method, the first word does not receive the prefix. Looking at my code, there is good reason for this as there is no space in front of the word. The code i use…
78282219
  • 85
  • 1
  • 8
1 2
3
84 85