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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.