BookmarkSubscribeRSS Feed
adrfinance
Obsidian | Level 7

Hello,

 

I have a dataset that has date entried in the following form:

 

2017-06-30

2017-06-30

 

and I would like to generate a new variable that has the year and the quarter in the following format:

 

YYYYQQ

 

i.e. the above entries would be:

 

2017Q2

 

How can I do that?

4 REPLIES 4
ed_sas_member
Meteorite | Level 14

Hi @adrfinance 

 

You can try the YYQ format:

data have;
	input date:YYMMDD10.;
	format date YYQ.;
	datalines;
2017-06-30
;
run;

 

ed_sas_member
Meteorite | Level 14

If the initial variable is a character variable and not a SAS date, you can use the input() function:

data have;
	input date $10.;	
	format date_q YYQ.;
	date_q = input(date, YYMMDD10.);
	datalines;
2017-06-30
;
run;
adrfinance
Obsidian | Level 7

Can you please take the values from a variable instead of using inline?

Kurt_Bremser
Super User

@adrfinance wrote:

Can you please take the values from a variable instead of using inline?


Datalines and input CREATE A VARIABLE, so your question is nonsense.

If you want to read existing values from a dataset, use the set statement, and the variables from the incoming dataset.

 

PS if you want code that is applicable to your data, it is necessary to supply that data, in a data step with datalines (see the gazillion examples her on the communities).

Use the "little running man" to post your code.