Using Macrovariable in a Date Variable (SAS) [duplicate] - sas

This question already has answers here:
Why won't my macro variable resolve?
(2 answers)
Closed 6 years ago.
I am programming in SAS and I am trying to figure out how to use a macrovariable within a date variable;
an example would be something like
%let year = 2015;
data test;
set;
date = '01JAN&year.'d;
run;
Thanks for your help!

Use double quotes and format the variable using the date9. format:
%let year = 2015;
data test;
format date date9.;
date = "01JAN&year."d;
run;

Related

How to convert dd-mmm-yyyy to date9 in sas

Want to convert date = 01-SEP-2021 to date = 01SEP2021. I used this code to convert:
New date = input(compress(date,'-'),date9.);
But that didn't work. I also used substring to remove the parts of day month and year but seems this method is a bit lengthy.
If the type is numeric and SAS already displays it as a date then it's a formatting issue.
proc datasets lib=work nolist;
modify have;
format date date9.;
quit;
Otherwise, it must be a character and you have to convert it to a date and display it in date9. format.
data want;
date = '01-SEP-2021';
new_date = input(date, anydtdte.); *anydtdte. automatically reads a variety of date formats;
format new_date date9.;
run;

How to reshape data wide to long [duplicate]

This question already has answers here:
SAS Data formatting (reverse proc transpose?)
(2 answers)
Closed 7 years ago.
I want to reshape data columns to rows
Initial Table as shown below
ID1 ID2 ID3 Name
----------------------------
I001 I002 I003 John
Desire Table like
ID Name
------------
I001 John
I002 John
I003 John
Can anyone help out?
Thanks lots!!
One way to do this is to set up an array of IDs and loop through with an explicit OUTPUT statement.
data want;
set have;
array ids(3) id1-id3;
do i=1 to dim(ids);
ID=ids(i);
OUTPUT;
end;
run;
You can use PROC TRANSPOSE Make sure your data is sorted by NAME
proc transpose data=have out=want(rename=(_name_=ID));
by Name;
run;

convert datetime to date in proc-sql sas [duplicate]

This question already has answers here:
SAS 9.3 DATETIME VARIABLE FORMAT AS DATE
(8 answers)
Closed 8 years ago.
I'm trying to convert datetime22.3 variable to ddmmmyy10. in proc sql, this is giving me ****** in the output column.
How can I get the correct values in the output column?
You need to convert original SAS DATETIME value (think of it as data type) to SAS DATE value using DATEPART() function and apply appropriate format:
proc sql;
create table work.abc
as select DISTINCT a.Account_Id,
DATEPART(a.Billing_Dt) format ddmmyy10. as Bill_date
from abc table;
quit;

SAS text to date format

I have a very basic question in SAS
I have the following macro variable which is in text format, I want to convert to a date format...not too sure how to do this:
%let mydt = 06/30/1999;
How do I convert this into a date format so that^
%let mydtnew = "06/30/1999"d;
Thanks for your help...
Try this - is that what you were after?
%let mydt = 06/30/1999;
%let mydt2 = %sysfunc(inputn(&mydt,mmddyy10.));
%put &mydt2;
Note that in SAS, date literals should follow DDMMMYYYY (date9.) format.
So, you could use something like this:
%let mydtnew = %sysfunc(putn(%sysfunc(inputn(&mydt., MMDDYY10.)), date9.));
The inner most %sysfunc is telling SAS that the date is in mm/dd/yyyy format - at this stage that text date is stored as a number. The outer %sysfuncis telling SAS to take the numeric date and format it into date9. format.
Note: mydtnew will store date without the double quotes or the d at the end. Usuall practice is to add the quotes and the d at the time you use the macro, such as
where account_open_date>="&mydtnew."d

Automating the starting date with a macro variable in SAS

Background:
I have a code that pulls transactional data starting at the beginning of the current calendar quarter, but from an year ago.
For example, if I run the code today (August 16, 2013) it will have to pull all the data from July 1, 2012 onwards.
Problem:
I want to automate the starting date for the data pull with a macro variable.
So far, I'm stuck here:
%let ThisYear = %Sysfunc(Date(), YEAR.);
%let LastYear= %eval(&ThisYear-1); /* I get the starting year */
%let QTR_start_month= %eval(3*%Sysfunc(Date(), qtr.)-2); /* this gives me the current quarter starting month. If I run it in August, it outputs 7 for July */
%let start_date=%str(01/%Sysfunc(month(&QTR_start_month))/&lcy);
The final macro variable outputs the date which I want, but in a format which is not recognized by SAS.
I will greatly appreciate any help.
Many thanks in advance!
You can either input that date to a date format, or construct it like a SAS date literal ('01JUL2013'), DDMONYY(YY), or construct it as a date value directly.
INTNX is probably your best option here to construct it; you don't need all that work.
%let start_date = %sysfunc(intnx(Quarter,%sysfunc(date()),-4),DATE9.);
%put &start_date;
You can leave DATE9. to use it as a date literal, or remove the ,DATE9. to get the numeric value that can be used directly. You would use this as "&start_Date."d to use the date literal.
This should do the job.
data test;
format todays_date starting_qtr date9.;
todays_date=today();
/*this takes today's date and rolls back 4 qtrs and sets that date to the first day of that quarter*/
starting_qtr = intnx('qtr',todays_date,-4,'b');
/*so, running this code today, 16AUG2013 would yield starting_qtr=01JUL2012 */
call symputx('start_date', put(starting_qtr, date9.));
run;
%put &start_date.;