Hi,
i am trying to create a SAS-formated date variable from a Character variable that contains information about the quarter and year.
Can someone help me with this?
Data:
obs char.var_(quarter_year) SAS-format_date_QYear_variable
1 Q1_2004 ?
2 Q2_2004 ?
3 Q3_2004 ?
4 Q4_2004 ?
5 Q1_2005 ?
6 Q2_2005 ?
7 Q3_2005 ?
.. .. ..
Thanks!
HI @Geo321 Are you asking for something like this?
data have;
input obs charvar $;* date_QYear_variable $;
cards;
1 Q1_2004 ?
2 Q2_2004 ?
3 Q3_2004 ?
4 Q4_2004 ?
5 Q1_2005 ?
6 Q2_2005 ?
7 Q3_2005 ?
;
data want;
set have;
date_QYear_variable=input(cats(scan(charvar,-1,'_'),scan(charvar,1,'_')),yyq6.);
qtr=qtr(date_QYear_variable);
year=year(date_QYear_variable);
format date_QYear_variable yyq6.;
run;
HI @Geo321 Are you asking for something like this?
data have;
input obs charvar $;* date_QYear_variable $;
cards;
1 Q1_2004 ?
2 Q2_2004 ?
3 Q3_2004 ?
4 Q4_2004 ?
5 Q1_2005 ?
6 Q2_2005 ?
7 Q3_2005 ?
;
data want;
set have;
date_QYear_variable=input(cats(scan(charvar,-1,'_'),scan(charvar,1,'_')),yyq6.);
qtr=qtr(date_QYear_variable);
year=year(date_QYear_variable);
format date_QYear_variable yyq6.;
run;
Hi @Geo321 ,
You could use YYQ() function
data have;
input obs charvar $;* date_QYear_variable $;
cards;
1 Q1_2004 ?
2 Q2_2004 ?
3 Q3_2004 ?
4 Q4_2004 ?
5 Q1_2005 ?
6 Q2_2005 ?
7 Q3_2005 ?
;
run;
data want;
set have;
y = input(scan(charvar,-1,"_"),best32.); drop y;
q = input(scan(charvar,-2,"Q_"),best32.); drop q;
format date_QYear_variable yyq6.;
date_QYear_variable = YYQ(y,q); /* it will give you the beginning of a quarter*/
run;
All the best
Bart
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.