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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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;
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Geo321
Calcite | Level 5
This also works. Thanks!
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
  • 4 replies
  • 3097 views
  • 0 likes
  • 3 in conversation