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

I have a date variable in YYMMDD10. format. e.g. 2017-06-26, I want to create another variable with Quarter and Year  of the date variable .e.g Q2 2017 (please note the space in between). Some of the values for the date variable is missing and I cant delete rows with missing values.

 

Following code only works when there is no missing date values and  returns quarter as Year-Quarter . eg. 2017Q2 

data want; set have; 

EndQuarter=put(EndDate, yyq.);

run;

 

But For missing date values, following code did not work.

data want; set have;

if EndDate=. then EndQuarter=.;

else EndQuarter=put(EndDate, yyq.);

run;

 

Any suggestion for changes?

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
The dollar sign means that your date variable is not a SAS date, but it's a character. So you first have to import it as a date using INPUT and then convert it to YYQ using PUT, or apply the format. This is easy to see in the future, because it has the $ sign in front of the format and that's only true for character formats, not numeric formats.

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

Your code works fine for me

 

data w;

enddate= input('2017-06-26',yymmdd10.);
length EndQuarter $10;
if EndDate=. then EndQuarter=' ';

else EndQuarter=put(EndDate, yyq6.);
output;
enddate=.;

if EndDate=. then EndQuarter=' ';

else EndQuarter=put(EndDate, yyq6.);
output;
format enddate yymmdd10.;
run;
SASKiwi
PROC Star

Why do you need to define your quarter year date as character? You can just leave it as a SAS date and then apply the date format you want:

 

EndQuarter=EndDate;
format EndQuarter yyq.;
d0816
Quartz | Level 8

I got this error with the suggested code:

The format $YYQ was not found or could not be loaded.

Reeza
Super User
The dollar sign means that your date variable is not a SAS date, but it's a character. So you first have to import it as a date using INPUT and then convert it to YYQ using PUT, or apply the format. This is easy to see in the future, because it has the $ sign in front of the format and that's only true for character formats, not numeric formats.
d0816
Quartz | Level 8

My variable was a string variable , so first I converted to numeric, as Reeza suggested. That did the trick. And then applied the format.

ballardw
Super User
data want; set have; 

if EndDate=. then EndQuarter=.; <= creates endquarter as numeric

else EndQuarter=put(EndDate, yyq.); <= attempts to put non-numeric value Q into a number.

run;

Try

 

data want; 
   set have; 
   length Enddate $ 6.;
   if EndDate=. then EndQuarter= ' ';

   else EndQuarter=put(EndDate, yyq.);

run;

Though for most purposes I would just use the format yyq. for Enddate .

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 18110 views
  • 3 likes
  • 5 in conversation