BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
---
Fluorite | Level 6 ---
Fluorite | Level 6

Hi there,

 

I have a piece of code that works if I do this

 

%LET sd = '1Jul2020'd;

PROC SQL;
CREATE TABLE xxx AS SELECT DISTINCT
xxx
FROM
xxx
WHERE 
date BETWEEN &sd. AND INTNX('month', &sd., 3, 'same') - 1;
QUIT;

 

But if I do

 

DATA _NULL_;
start_date = MDY(%SUBSTR(&quarter., 6, 1) * 3 - 2, 1, %SUBSTR(&quarter., 1, 4));
CALL SYMPUT('start_date', start_date);
...
RUN;

 

And replace &sd. with &start_date., what passes through PROC SQL is a SAS date and returns incorrect/nil data - how do I turn my newly created start_date variable into something that's useable?

 

I've googled this and tried a few things and can't find anything that I can use unfortunately

 

Thanks for your help

1 ACCEPTED SOLUTION

Accepted Solutions
---
Fluorite | Level 6 ---
Fluorite | Level 6

Just played with the codes a bit more and found that this would work - what I hadn't tried was using double rather than single quotes... 😑 feels like banging my head against the wall

 

DATA _NULL_;
	date = MDY(SUBSTR("&quarter.", 6, 1) * 3, 1, SUBSTR("&quarter.", 1, 4));
	CALL SYMPUTX('date', PUT(date, DATE9.));
RUN;

PROC SQL;
	CREATE TABLE xxx AS SELECT DISTINCT
	    xxx
	FROM
		xxx
	WHERE 
		date BETWEEN "&date."d AND "&date."d;
QUIT;

 

View solution in original post

6 REPLIES 6
SASKiwi
PROC Star

Try this:

DATA _NULL_;
start_date = MDY(input(SUBSTR("&quarter.", 6, 1),1.) * 3 - 2, 1, input(SUBSTR("&quarter.", 1, 4),4.));
CALL SYMPUT('start_date', start_date);
...
RUN;

If this doesn't work then you will need to post how you assign QUARTER. 

---
Fluorite | Level 6 ---
Fluorite | Level 6

Thanks for your prompt response

 

I've forgotten about the quotes, thanks for pointing that out - however, adding them still gave me the same outcome

 

This is my quarter variable

 

%LET quarter = 2020Q3;

Ultimately I was able to generate a start_date = 1 July 2020 even with my old code but this is 22097 in SAS without applying format and it's attempting to pass 22097 into the PROC SQL where statement rather than '1Jul2020'd

 

I tried turning the date into a character and then add ''d to it but none seem to work - any other advice would be greatly appreciated 

SASKiwi
PROC Star

So you are expecting 2020Q3 to convert to a SAS date of 01 July 2020?

---
Fluorite | Level 6 ---
Fluorite | Level 6
Yes among other things - I think there might be a date format that can convert more elegantly but it worked for me 🙂
---
Fluorite | Level 6 ---
Fluorite | Level 6

Just played with the codes a bit more and found that this would work - what I hadn't tried was using double rather than single quotes... 😑 feels like banging my head against the wall

 

DATA _NULL_;
	date = MDY(SUBSTR("&quarter.", 6, 1) * 3, 1, SUBSTR("&quarter.", 1, 4));
	CALL SYMPUTX('date', PUT(date, DATE9.));
RUN;

PROC SQL;
	CREATE TABLE xxx AS SELECT DISTINCT
	    xxx
	FROM
		xxx
	WHERE 
		date BETWEEN "&date."d AND "&date."d;
QUIT;

 

SASKiwi
PROC Star

@---  - This is a bit cleaner:

%let quarter = 2020Q3;

data test;                                     
   sasdate=input("&quarter",yyq6.);                           
   put sasdate date9.;              
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1507 views
  • 2 likes
  • 2 in conversation