SAS: Assign a macro - macros

How to assign a variable that contains a string to a macro?
I've put my question in the comments of code below:
%let HOLD_MONTHS=1;
data _null_;
if &HOLD_MONTHS=1 then hold='h1m';
*temp='model_h1m';
temp=catx('_','model',hold);
*how to assign 'model_h1m to the macro a?;
%let a = %str(temp );
run;
*now the following print "temp";
%put &a;

You can use call symput to assign the value of a variable in a data step, e.g.:
%let HOLD_MONTHS=1;
data _null_;
if &HOLD_MONTHS=1 then hold='h1m';
temp=catx('_','model',hold);
call symput('a',temp);
run;
%put &a;

Related

How to pass macro variable into macro variable name in SAS ?

%let val = ' run';
%macro rrun;
%put successfully run;
%mend;
%macro x;
%r%cmpres(&val.);
/* %rrun;*/
%mend;%x
I"m trying to pass macro variable into macro variable name in SAS. to run %rrun using %cmpres(&val.) passing value to macro name "rrun" I don't understand why this doesn't work. when compress &val is "run"
How can I solve this and still passing &val ?
Thanks,
Why do you have quotes in your macro variable?
166 %let val = ' run';
167 %put |%cmpres(&val)|;
|' run'|
If you want to dynamically generate the name of macro to call it works much easier if you first generate the name into a macro variable and then reference that macro variable. Otherwise you risk confusing the tokenizer.
%macro x;
%local mname;
%let mname=r%cmpres(&val);
%&mname;
%mend;
%x

SAS - Custom Date Format - YYYYMMDDhhmmss

I need to add a current date to the output I am exporting from SAS in the following format: filename_YYYYMMDDhhmmss.csv
I am creating a macro variable the following way:
%let date_human = %sysfunc(today(), YYYYMMDDn8.);
Does anybody know how to create a custom format for the date I have got? datetime20. gives an incorrect one.
Thank you.
Use the B8601 formats.
%let now=%sysfunc(datetime());
%let date_human=%sysfunc(putn(&now,B8601DN8))%sysfunc(timepart(&now),B8601TM6);
Two solutions:
1. Create a picture format with datetime directives using proc format
Here, we are creating a format called date_human. You can name this anything that you'd like.
proc format;
picture date_human other='%0Y%0M%0D%0H%0M%0S' (datatype=datetime);
run;
%let date_human = %sysfunc(datetime(), date_human.);
2. Mash together various macro variables
/* YYYYMMDD part */
%let yymmdd = %sysfunc(today(), yymmdd8.);
%let datetime = %sysfunc(datetime());
%let time = %sysfunc(putn(%sysfunc(timepart(&datetime.)), time8.0));
/* hhmmss part */
%let hour = %scan(&time., 1, :);
%let min = %scan(&time., 2, :);
%let sec = %scan(&time., 2, :);
/* Put it all together */
%let date_human = &yymmdd.&hour.&min.&sec.;
Use picture in proc format:
proc format;
*custom format for the datetime values;
picture cust_dt other = '%0Y%0m%0d%0H%0M%0S' (datatype=datetime);
run;
** put into macro **;
data test;
dt = datetime();
call symputx("dt",strip(put(dt,cust_dt.)));
run;
%put &dt.;
** use macro for filename **;
data file_&dt.; set sashelp.class;
run;

SAS Error trying to loop through multiple datasets

I'm trying to run some code which will hopefully concatenate multiple months or years worth of data. I am trying to figure out when a field was populated with a value. I.e. there is field XYZ in my data set and it is populated with value A in November 2016. If I run my code from Jan - Dec I would like a new field populated with the date that SAS encounters a non-blank value in that field.
Here's my code:
options mprint symbolgen source mlogic merror syntaxcheck ;
%macro append_monthly(iStart_date=, iEnd_date=);
%local tmp_date i;
%let tmp_date = %sysfunc(intnx(month,&iStart_date,0,beginning)) ;
%do %while (&tmp_date le &iEnd_date);
%let i = %sysfunc(sum(&tmp_date),yymmn4.);
%put &i.;
%let tmp_date = %sysfunc(intnx(month,&tmp_date,1,beginning)) ;
libname note "my.qualifiers.fords.note&i." disp=shr;
data new ;
set note.file ;
%if ln_note_crbur_date_delinq ne '' %then spc_cmt_date = &i.;
run;
%end;
%mend;
%append_monthly(iStart_date=%sysfunc(mdy(5,1,2016)), iEnd_date=%sysfunc(mdy(10,1,2016)) );
LIBNAME _ALL_ CLEAR;
Here's a sample from log with errors :
SYMBOLGEN: Macro variable TMP_DATE resolves to 20606
SYMBOLGEN: Macro variable IEND_DATE resolves to 20728
MLOGIC(APPEND_MONTHLY): %DO %WHILE(&tmp_date le &iEnd_date) condition is TRUE; loop will iterate again.
MLOGIC(APPEND_MONTHLY): %LET (variable name is I)
SYMBOLGEN: Macro variable TMP_DATE resolves to 20606
MLOGIC(APPEND_MONTHLY): %PUT &i.
SYMBOLGEN: Macro variable I resolves to 1606
1606
MLOGIC(APPEND_MONTHLY): %LET (variable name is TMP_DATE)
SYMBOLGEN: Macro variable TMP_DATE resolves to 20606
MPRINT(APPEND_MONTHLY): spc_cmt_date = 1605 run;
SYMBOLGEN: Macro variable I resolves to 1606
MPRINT(APPEND_MONTHLY): libname note "my.qualifiers.fords.note1606" disp=shr;
ERROR: Unable to clear or re-assign the library NOTE because it is still in use.
ERROR: Error in the LIBNAME statement.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.NEW may be incomplete. When this step was stopped there were 0 observations and 622 variables.
WARNING: Data set WORK.NEW was not replaced because this step was stopped.
NOTE: The DATA statement used 0.01 CPU seconds and 49483K.
NOTE: The address space has used a maximum of 4292K below the line and 240388K above the line.
I can't figure out why this isn't working. Maybe this could work using Proc append.
Basically, I just want my output with a field that returns a date in the form of YYMM for when field ln_note_crbur_date_delinq was non-blank.
Any help would be greatly appreciated
I'd guess the reason for your error is that the handle is not being cleared on your source file before the next libname statement tries to re-assign.
An easy fix would be to use a different alias (libref) each time, as follows:
libname note&i "my.qualifiers.fords.note&i." disp=shr;
Then adjust your data step like so:
data new ;
set note&i..file ;
The next part appears to be confusion between macro logic and data step. Simply remove the % symbols as follows:
if ln_note_crbur_date_delinq ne '' then spc_cmt_date = &i.;
Finally, add a proc append before the %end as follows:
proc append base=work.final data=new; run;
If work.final does not exist, it will be created in the same format as new.
EDIT:
following discussion in comments, here is a revised approach:
%macro append_monthly(iStart_date=, iEnd_date=);
%local tmp_date i set_statement;
%let tmp_date = %sysfunc(intnx(month,&iStart_date,0,beginning)) ;
%do %while (&tmp_date le &iEnd_date);
%let i = %sysfunc(sum(&tmp_date),yymmn4.);
%let tmp_date = %sysfunc(intnx(month,&tmp_date,1,beginning)) ;
%let set_statement=&set_statement &i..file;
libname note&i "my.qualifiers.fords.note&i." disp=shr;
%end;
data new ;
set &set_statement;
if ln_note_crbur_date_delinq ne '' then spc_cmt_date = &i.;
run;
%mend;
%append_monthly(iStart_date=%sysfunc(mdy(5,1,2016)), iEnd_date=%sysfunc(mdy(10,1,2016)) );
LIBNAME _ALL_ CLEAR;

SAS - Macro calling another macro

I have a problem with SAS. More precisely with calling macro, which is inside another macro. Here is the example.
data TEST_1;
do i = 1 to 100;
a=i**2;
output;
end;
run;
data TEST_2;
do i = 1 to 100;
b=i**3;
output;
end;
run;
%macro macro_in(file_a);
data result1;
set &file_a;
c=a+1;
run;
%mend;
%macro_in(TEST_1);
%macro macro_out(file_b);
data result2;
set &file_b._2;
d=a-1;
run;
data _null_;
do i = 1 to 2;
call execute(COMPRESS('%macro_in(' || &file_b || '_' || i || ')'));
output;
end;
run;
%mend;
%macro_out(TEST);
First macro works completely fine, however there is a slight problem with variable file_b i the second macro (Code cannot use it as an argument to the inner macro). Thanks for any help!
When calling macro_in from macro_out you do not need a datastep, you can use the macro language:
%macro macro_out(file_b);
data result2;
set &file_b._2;
d=a-1;
run;
%do i = 1 %to 2;
%macro_in(&file_b._&i);
%end;
%mend;

Attribute Change of Macro Variable in SAS

I have a macro variable like &a having value of (1234.45)*. I am trying to replace the ( and ) from the macro and replace them with a negative mark since its a negative number.
%let a=(1234.45)
Some of the options which I have appplied are
%macro test1;
%if %substr(&a,1,1) = '(' %then %do;
%let b=%substr(&a,1,'-')
%end;
%mend;
%test1
This is numeric conversion and best handled in a data step. If for some reason you really need a macro variable, use SYMPUT.
%let a=(1234.45);
data _null_;
x=input("&a.",comma10.);
call symputx("b",x);
run;
%put &=a &=b;