BookmarkSubscribeRSS Feed
Aexor
Lapis Lazuli | Level 10

I have below table

 

table have

id  trans_date

1    23-11-2021

2    20-10-2021

3    15-08-2021

4    29-04-2021

8    10-03-2021

9    15-05-2021

10  12-03-2021

11   03-06 -2021

2    15-08-2021

3    15-08-2021

4     16-01-2021

 

I want all ids of  transactions done in a particular quarter. 

e.g for third quarter ids are 8,10,4

 

 

 

 

 

11 REPLIES 11
ballardw
Super User

Which "particular quarter"? You need to specify what quarter you mean. If the "quarter" does not correspond to a calendar quarter you also need to specify which dates make up your quarter.

 

Is the trans_date variable actually a SAS date value? That would mean the variable is 1) numeric and 2) with a format assigned like ddmmyy10. or similar.

 

 

Aexor
Lapis Lazuli | Level 10
here we need ids of transaction in below quater category
1st quarter(Jan-mar), 2nd quarter(apr-jun) , 3rd quarter(jul-sept),
4th quarter(oct-dec) .

All the transactions made in between jan to mar will come under quarter 1
ballardw
Super User

So, what does the output look like?

And still have not answered the question whether the variable is actually as SAS date.

 

There are lots of functions and formats that work with SAS dates but do not work with random text values like '21-10-2020'.

Aexor
Lapis Lazuli | Level 10
My bad. Yes these are sas dates
Reeza
Super User
where qtr(trans_date) = 3;

Assumes you have a SAS date, which means a numeric variable with a date format.
novinosrin
Tourmaline | Level 20

Hi @Aexor  Please use the below as template. The Proc summary groups by the formatted value which is rather more elegant imho albeit consumes ton of memory to sort and dump in the classfication group in memory addresses very similar to a Hash table. More details I would leave it to sages @ballardw / @Reeza  if they will as I am lazy


data have;
 input id  trans_date :ddmmyy10.;
 format trans_date ddmmyy10.;
 tx_amount=rand('uniform')*1000;
 cards;
1    23-11-2021

2    20-10-2021

3    15-08-2021

4    29-04-2021

8    10-03-2021

9    15-05-2021

10  12-03-2021

11   03-06-2021

2    15-08-2021

3    15-08-2021

4     16-01-2021
;

proc sql;
 create table want as
 select intnx('qtr',trans_date,0,'e') as year_qtr format=yyq6.,
  sum(tx_amount) as sum
  from have
  group by year_qtr;
quit;
/*OR*/
proc summary data=have nway;
 class trans_date;
 var tx_amount;
 format trans_date yyq6.;
 output out=want(drop=_:) sum=;
run;

 

 

Aexor
Lapis Lazuli | Level 10
Can you please explain this ? and the need of tx_amount=rand('uniform')*1000;
Reeza
Super User
It's just a method to create sample data to solve your problem. It's randomly generating the tx_amount value between 0 and 1000. There's a newer function, Rand('integer, 1, 1000) which will give similar answers but just integers, no decimals.
novinosrin
Tourmaline | Level 20

Oh I was just making a mock sample data in which I wanted to have a variable called tx_amount with random numbers. 🙂

asuman
Obsidian | Level 7
data test;
input id trans_date;
informat trans_date ddmmyy10.;
format trans_date ddmmyy10.;
cards;
1 23-11-2021
2 20-10-2021
3 15-08-2021
4 29-04-2021
8 10-03-2021
9 15-05-2021
10 12-03-2021
11 03-06-2021
2 15-08-2021
3 15-08-2021
4 16-01-2021
;
run;
data test;
set test;
quater=qtr(trans_date);
run;
sbxkoenk
SAS Super FREQ
data table_have;
input id trans_date;
informat trans_date ddmmyy10.;
  format trans_date ddmmyy10.;
cards;
1    23-11-2021
2    20-10-2021
3    15-08-2021
4    29-04-2021
8    10-03-2021
9    15-05-2021
10   12-03-2021
11   03-06-2021
2    15-08-2021
3    15-08-2021
4    16-01-2021
;
run;

data table_want;
 set table_have;
 Quarter1=QTR(trans_date);
 Quarter2=put(trans_date,YYQZ6.);
run;
/* end of program */

Koen

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 11 replies
  • 771 views
  • 5 likes
  • 6 in conversation