Desktop productivity for business analysts and programmers

Using a PUT in a WHERE statement

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 129
Accepted Solution

Using a PUT in a WHERE statement

Hi All,

 

I do reports that use the previous Sunday as the end date. I can calculate the Sunday based on the data _null_ step, but how do I now use that in the Where Week_end_dt =  'Sunday.'d;

 

 

data _null_; 
    ref=today();
    sunday = intnx('week1.2',ref,-1,'End');
    format _all_ Date10.; 
    put Sunday;
    run; 



	
Data work.TT_Weekly_report;
	Set Data.Employee;
	Keep Week_end_dt EVENT_DT Cluster Queue_CD TOTAL_CALLS TALK_TIME MBL;
	Where Week_end_dt =  'Sunday.'d;
	RUN;
		

 

Cheers

 

Dean

 

 


Accepted Solutions
Solution
‎09-13-2017 01:46 AM
Super User
Posts: 23,932

Re: Using a PUT in a WHERE statement

Macro variables, in this usage are text replacement, literally like find and replace.

INTNX is used to align the date (4th parameter) to the beginning of the week. 

 

data _null_;
call symputx('DateVar', put(intnx('week', today(), 0, 'b'), date9.));
run;

Data work.TT_Weekly_report;
	Set Data.Employee;
	Keep Week_end_dt EVENT_DT Cluster Queue_CD TOTAL_CALLS TALK_TIME MBL;
	Where Week_end_dt =  "&DateVar"d;
	RUN;

Or you could just apply that in your WHERE clause directly.

 

Data work.TT_Weekly_report;
	Set Data.Employee;
	Keep Week_end_dt EVENT_DT Cluster Queue_CD TOTAL_CALLS TALK_TIME MBL;
	Where Week_end_dt =  intnx('week', today(), 0, 'b');
	RUN;

View solution in original post


All Replies
PROC Star
Posts: 1,332

Re: Using a PUT in a WHERE statement

Good start! This is where you need SAS macro variables. They are always character, so you need to convert your date into a character version.

 

In your data _null_ step, switch out your format and put statements, and instead use

 

call symput('DateVar', put(sunday, date9.));

 

to load the formatted date into a macro variable named DateVar.

And then in your second step, use

 

Where Week_end_dt =  "&Sunday."d;

 

You need double quotes to allow the substitution.

Tom

Frequent Contributor
Posts: 129

Re: Using a PUT in a WHERE statement

Thanks Tom,

 

I must still be missing something sorry. I have changed the code to:

 

 

data _null_; 
       call symput('DateVar', put(sunday, date9.));
    run; 

%PUT &Sunday.;

Data work.TT_Weekly_report;
	Set Data.Employee;
	Keep Week_end_dt EVENT_DT Cluster Queue_CD TOTAL_CALLS TALK_TIME MBL;
	Where Week_end_dt =  "&Sunday."d;
	RUN;

and I get this error

 

29          Where Week_end_dt =  "&Sunday."d;
NOTE: Line generated by the macro variable "SUNDAY".
29           "" 10SEP2017"
                __
                22
                76
ERROR: Syntax error while parsing WHERE clause.
ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &, *, **, +, -, /, <, <=, <>, =, >, >=, AND, EQ, GE, GT, LE,
              LT, NE, NOT, OR, ^, ^=, |, ||, ~, ~=. 

ERROR 76-322: Syntax error, statement will be ignored.

 

 

Thanks for your help.

 

Dean

 

PROC Star
Posts: 1,332

Re: Using a PUT in a WHERE statement

Sorry, brain fart.

 

The name of the macro variabel is DateVar. I forgot to change it in the second reference.

 

Use &DateVar. instead of &Sunday.

 

Tom

Frequent Contributor
Posts: 129

Re: Using a PUT in a WHERE statement

All good Tom - appreciate the help.

 

 

I'm now getting no data returned due to the date being blank (if I read the note properly.

 

NOTE: There were 0 observations read from the data set SDPTEL.EMPLOYEE_TELEPHONY.
      WHERE Week_end_dt='        .'D;

 

 

 

data _null_;
	call symput('DateVar', put(sunday, date9.));
run;

%PUT &DateVar;


Data work.TT_Weekly_report;
	Set Data.Employee;
	Keep Week_end_dt EVENT_DT Cluster Queue_CD TOTAL_CALLS TALK_TIME MBL;
	Where Week_end_dt =  "&DateVar."d;
RUN;

 

 

 

Super User
Posts: 23,932

Re: Using a PUT in a WHERE statement

There's no SET statement in your data _null_ step.

Frequent Contributor
Posts: 129

Re: Using a PUT in a WHERE statement

Hi Reeza - I'm not sure what you mean by not having a SET statement in the Data _NULL_ step.
Super User
Posts: 23,932

Re: Using a PUT in a WHERE statement

data _null_; 
call symput('DateVar', put(sunday, date9.));
run;

 

You can't create a macro variable out of nothing. You're referencing the variable SUNDAY in the data step but there's no data set with a variable named SUNDAY and 'sunday' doesn't have any meaning so I'm not sure what you're trying to do here.

 

You had it working before, but you changed it for some reason?

Solution
‎09-13-2017 01:46 AM
Super User
Posts: 23,932

Re: Using a PUT in a WHERE statement

Macro variables, in this usage are text replacement, literally like find and replace.

INTNX is used to align the date (4th parameter) to the beginning of the week. 

 

data _null_;
call symputx('DateVar', put(intnx('week', today(), 0, 'b'), date9.));
run;

Data work.TT_Weekly_report;
	Set Data.Employee;
	Keep Week_end_dt EVENT_DT Cluster Queue_CD TOTAL_CALLS TALK_TIME MBL;
	Where Week_end_dt =  "&DateVar"d;
	RUN;

Or you could just apply that in your WHERE clause directly.

 

Data work.TT_Weekly_report;
	Set Data.Employee;
	Keep Week_end_dt EVENT_DT Cluster Queue_CD TOTAL_CALLS TALK_TIME MBL;
	Where Week_end_dt =  intnx('week', today(), 0, 'b');
	RUN;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 265 views
  • 0 likes
  • 3 in conversation