I'm trying to pass file name to a macro. The macro runs once a month, therefore,I'm trying to store the output file with a month prefix. In the current code someone has to manually provide a file name every month (Sep17_Sales, Oct17_Sales etc.). I want to automate this so that SAS generates files with the name of the month prefixed to the data file.
Macro:
%macro sales (outdata = , dt =);
Current Code
%Sales(Outdata = Sep17_Sales, dt = '2017-09-01');
%Sales(Outdata =Oct17_Sales, dt ='2017-10-01');
My approach:
data _null_;
current_date = today();
current_month = intnx('month', current_date, 0, "Begginning");
Name = "_Sales";
Result = put(current_month, monyy7.) || name;
run;
%Sales(Outdata=Result, dt='2017-10-01');
When I try to pass the parameter, it throws error. I tried changing Result to %Let Result and pass a reference &Result to the macro but it also fails.
Any suggestion how to solve this? Thank you for all the help!!
What you are doing there is assigning a value to a data step variable called Result. The name Result doesn't mean anything outside the context of that datastep and therefore does not resolve to anything when you call your macro. What you are doing instead is telling your macro that your output file should be called "Result".
You could fix that by replacing your Result= line with call symput('Result',put(current_month, monyy7.) || name);, which effectively creates a macro variable called "Result", then call your sales macro like so: ``%Sales(Outdata=&Result, dt='2017-10-01');
OR, you could scratch all that and simply call your macro like this:
%sales(outdata=%sysfunc(today(),monyy7.)_Sales, dt='2017-10-01');
Going further, assuming the second argument (dt) is always meant to be the first day of the month formatted as yyyy-mm-dd and enclosed in single quotes (although if that is the case I see little use in specifying it as a parameter of the macro), you could make the call even more dynamic:
%sales(outdata=%sysfunc(today(),monyy7.)_Sales, dt=%str(%')%sysfunc(intnx(month,%sysfunc(today()),0,B),E8601DA.)%str(%'));
if that date can be enclosed in double quotes, this can be simplified a little as:
%sales(outdata=%sysfunc(today(),monyy7.)_Sales, dt="%sysfunc(intnx(month,%sysfunc(today()),0,B),E8601DA.)");
Related
I have a project with multiple programs. Each program has a proc SQL statement which will use the same list of values for a condition in the WHERE clause; however, the column type of one database table needed is a character type while the column type of the other is numeric.
So I have a list of "Client ID" values I'd like to put into a macro variable as these IDs can change, and I would like to change them once in the variable instead of in multiple programs.
For example, I have this macro variable set up like so and it works in the proc SQL which queries the character column:
%let CLNT_ID_STR = ('179966', '200829', '201104', '211828', '264138');
Proc SQL part:
...IN &CLNT_ID_STR.
I would like to create another macro variable, say CLNT_ID_NUM, which takes the first variable (CLNT_ID_STR) but removes the quotes.
Desired output: (179966, 200829, 201104, 211828, 264138)
Proc SQL part: ...IN &CLNT_ID_NUM.
I've tried using the sysfunc, dequote and translate functions but have not figured it out.
TRANSLATE doesn't seem to want to allow a null string as the replacement.
Below uses TRANSTRN, which has no problem translating single quote into null:
1 %let CLNT_ID_STR = ('179966', '200829', '201104', '211828', '264138');
2 %let want=%sysfunc(transtrn(&clnt_id_str,%str(%'),%str())) ;
3 %put &want ;
(179966, 200829, 201104, 211828, 264138)
It uses the macro quoting function %str() to mask the meaning of a single quote.
Three other ways to remove single quotes are COMPRESS, TRANSLATE and PRXCHANGE
%let CLNT_ID_STR = ('179966', '200829', '201104', '211828', '264138');
%let id_list_1 = %sysfunc(compress (&CLNT_ID_STR, %str(%')));
%let id_list_2 = %sysfunc(translate(&CLNT_ID_STR, %str( ), %str(%')));
%let id_list_3 = %sysfunc(prxchange(%str(s/%'//), -1, &CLNT_ID_STR));
%put &=id_list_1;
%put &=id_list_2;
%put &=id_list_3;
----- LOG -----
ID_LIST_1=(179966, 200829, 201104, 211828, 264138)
ID_LIST_2=( 179966 , 200829 , 201104 , 211828 , 264138 )
ID_LIST_3=(179966, 200829, 201104, 211828, 264138)
It really doesn't matter that TRANSLATE replaces the ' with a single blank () because the context for interpretation is numeric.
I'm new to SAS. I encurred into a problem when trying to declare a macro variable with the result of some operation as value.
data _null_;
%let var1 = 12345;
%let var2 = substr(&var1., 4,5);
run;
I get that var2 has value substr(&var1., 4,5) (a string) instead of 45 as I would like. How to make the variable declaration evaluate the function?
Sorry it the question is trivial. I looked in the documentation for a bit but couldn't find an answer.
There is a macro equivalent called %substr() which can be used as follows:
%let var1 = 12345;
%let var2 = %substr(&var1., 4,2);
%put var2 = &var2;
Note that the data and run statements are not required for macro language processing and the 3rd argument to %substr() (and substr()) specifies the length you want, not the position of the last character, which is why I used 2 instead of 5.
Edit: Also, if there is no macro equivalent then you could use %sysfunc() to make use of the data step function in macro code. See the documention for full details as there are some quirks, such as not using quotes and a few exceptions to the list of data step functions that can be used.
I am working with SAS and want to record variable which with over 50+ different qualitative dummies. For example, the state of the U.S.
In this case, I just want to reduce them into 4 or 5 levels dummy as quantitative variable.
I get several ideaS, for example to use if/else statement, however, the problem is that i have to write down and specify each of area name in SAS and the code looks like super heavy.
Is there any other ways to do that without redundant code? Or to avoid write each specific name of variable? In SAS.
Any ideas are appreciated!!
Method 1:
Use IN, but you still have to list the variables. You can also do it via a format, but you have to define the format first anyways.
if state in ('AL', 'AK', 'AZ' ... etc) then state_group = 1;
else if state in ( .... ) then state_group = 2;
Method 2:
For a format, you create format using PROC FORMAT and then apply it.
proc format;
value $ state_grp_fmt
'AL', 'AK', 'AZ' = 1
'DC', 'NC' = 2 ;
run;
And then you can use it with a PUT statement.
State_Group = put(state, state_grp_fmt);
I'm running a process that lists jobs I want to check the modification date on. I list the jobs in a dataset and then pass these to macro variables with a number.
e.g.
Data List_Prep;
Format Folder
Code $100.;
Folder = 'C:\FilePath\Job ABC'; Code = '01 Job Name.sas'; Output;
Folder = 'C:\FilePath\Job X&Y'; Code = '01 Another Job.sas'; Output;
Run;
%Macro List_Check();
Data List;
Set List_Prep;
Job + 1;
Call Symput (Cats("Folder", Job), Strip(Folder));
Call Symput (Cats("Code", Job), Strip(Code));
Run;
%Put Folder1 = &Folder1;
%Put Folder2 = &Folder2;
%MEnd;
%List_Check;
It prints the %Put statement just fine for foler 1, but folder 2 doesn't work right.
Folder1 = C:\FilePath\Job ABC
WARNING: Apparent symbolic reference Y not resolved.
Folder2 = C:\FilePath\Job X&Y
When I then go in to a loop to check the datasets, again, it work, so looks for Folder1, Code1 etc, but I still get the warnings.
How can I stop these warnings? I've tried %Str("&") instead, but still get the issue.
The %superq() macro function is a great way to mask macro triggers that are already in a macro variable. You could either remember to quote the values when using them,
%put Folder1 = %superq(Folder1) ;
or you could adjust your process to quote them right after creating them.
data List_Prep;
length Folder Code $100;
Folder = 'C:\FilePath\Job ABC'; Code = '01 Job Name.sas'; Output;
Folder = 'C:\FilePath\Job X&Y'; Code = '01 Another Job.sas'; Output;
run;
data List;
set List_Prep;
Job + 1;
length dummy $200 ;
call symputx(cats("Folder", Job), Folder);
dummy = resolve(catx(' ','%let',cats("Folder", Job),'=%superq(',cats("Folder", Job),');'));
call symputx(cats("Code", Job), Code);
dummy = resolve(catx(' ','%let',cats("Code", Job),'=%superq(',cats("Code", Job),');'));
drop dummy;
run;
P.S. Don't use FORMAT to define variables. Use statements like LENGTH or ATTRIB that are designed for defining variables. FORMAT is for attaching formats to variable, not for defining them. The only reason that using FORMAT worked is that it had the side effect of SAS defining the variable's type and length to match the format that you attached to it because it was the first place you referenced the variable in the data step.
You can prevent SAS from trying to resolve the ampersand in the value by using the %superq function
%put Folder2 = %superq(Folder2);
I want to remove multiple blanks from a string.
%let CDate = 25Mar2015;
data _null_;
call symput('TitleDate',cat(put("&CDate."d,monname9.),', ',year("&CDate."d)));
run;
%put &TitleDate; /* March, 2015 */
%put Title is &TitleDate; /* multiple blanks between 'is' and 'March' */
I tried compress: %put Title is %sysfunc(compress(&TitleDate)); But it returns Title is March without year part.
Very close, two modifications:
CALL SYMPUTX instead of CALL SYMPUT
WORDDATE20. format instead of the cat/put combinations
call symputx('TitleDate',put("&CDate."d,worddate20.));
EDIT (to answer question in comments):
The compress function takes 3 arguments, the first is mandatory and the last two are option.
The compress function doesn't work because what SAS is seeing is:
compress(March 25, 2015)
This is an invalid call to the compress function. I think the compress function would assume the second argument would be 2015 and I don't know what it does with the 25. I would actually expect it to generate an error, but it doesn't.
If you did want to use the compress function to pass in a character value you would need to quote it using the %quote function, but that gets rid of all the spaces and I think you just wanted to get rid of the leading spaces.
%put Title is %sysfunc(compress(%quote(&TitleDate.)));