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
Onyx | Level 15

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!

sas-innovate-2024.png

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.

 

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
  • 4 replies
  • 2159 views
  • 0 likes
  • 3 in conversation