DATA Step, Macro, Functions and more

SAS:how to extract year/month/date from a marco variable and add an integer?

Reply
Contributor
Posts: 47

SAS:how to extract year/month/date from a marco variable and add an integer?

[ Edited ]

proc sql;

select max(data_bsn_dt) into:date01 from tableA;

quit;

 

%put &date02;

print out:31DEC2017

 

%put year("&date01"d);

print out:year("31DEC2017"d)

 

 

I used code:year("&date01"d) to extract year or month before,but it does not work now..could some have time solve,thanks.

 

 

 

Even in the data step,it still not working..

data _nulll;
if year("&date01"d)=2017
then call execute('proc sql;
                   select * from tableB;');
run;
Super User
Posts: 2,068

Re: SAS:how to extract year/month/date from a marco variable and add an integer?

%let month= %sysfunc(month("&date01"d));
%let year= %sysfunc(year("&date01"d));
Contributor
Posts: 47

Re: SAS:how to extract year/month/date from a marco variable and add an integer?

Posted in reply to novinosrin

thank you for your answer..I want to let them work in the data step,like:

 

data _null_;

if year("&date01"d)=year("&date02"d)=year("&date03"d)

then...

run;

Super User
Posts: 2,068

Re: SAS:how to extract year/month/date from a marco variable and add an integer?


1 %let date01=31DEC2017;
2
3 data _nulll;
4 if year("&date01"d)=2017
5 then put 'ok';
6 run;

ok
NOTE: The data set WORK._NULLL has 1 observations and 0 variables.
NOTE: DATA statement used (Total process time):
real time 0.34 seconds

 

works fine 

Super User
Posts: 24,026

Re: SAS:how to extract year/month/date from a marco variable and add an integer?

Your PROC SQL is missing a QUIT but your code appears correct otherwise. 

 

Are you sure that’s what your macro variable is resolving to? Sometimes it’s not formatted and I can see that causing issues. Given you haven’t specified a format I’m pretty sure that’s what’s happening here, see test below. 

 

 

2A7B2DBF-A05A-4DB5-84C4-B3F985E4F30B.jpeg

 


@Geo- wrote:

proc sql;

select max(data_bsn_dt) into:date01 from tableA;

quit;

 

%put &date02;

print out:31DEC2017

 

%put year("&date01"d);

print out:year("31DEC2017"d)

 

 

I used code:year("&date01"d) to extract year or month before,but it does not work now..could some have time solve,thanks.

 

 

 

Even in the data step,it still not working..

data _nulll;
if year("&date01"d)=2017
then call execute('proc sql;
                   select * from tableB;');
run;

 

Super User
Posts: 10,594

Re: SAS:how to extract year/month/date from a marco variable and add an integer?

Try:

proc sql;
select max(data_bsn_dt) into:date01 from tableA;
quit;

data _null_;
if year(&date01) = 2017
then call execute('proc sql; select * from tableB;');
run;

In your example, you are %PUTting variable &date02 for control, but you use &date01 later in the code. %put &date01 and inspect it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 5 replies
  • 145 views
  • 0 likes
  • 4 in conversation