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

Hi I want to create "July 2nd Week" in new column,Below is the sample code i tried but i stuck in adding ('th' 'st') dynamically  or is there any alternative way to create please help me on this .

 

data want;
format Today date9. month $3. Week $3. Cat $10.;
Today=today();
month=put(month(Today),monname3.);
Week=put(WEEKDAY(Today),weekday.);
Cat= put(catx(' ',month,Week,'Week'),$10.);
run;

 

Thanks,

Siva

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

I have an ordinal format here you can use:

 

https://gist.github.com/statgeek/6c885f6aea18dbd6caf8

 

data ordinal_format;
	length label $8.;
  do number=1 to 300;
    numstring = put(number, 16. -l);
    if prxmatch('/(?<!1)1\s*$/',numstring) then ending='st';
    else if prxmatch('/(?<!1)2\s*$/',numstring) then ending='nd';
    else if prxmatch('/(?<!1)3\s*$/',numstring) then ending='rd';
    else ending = 'th';
    ordstring =catt(numstring, ending);

	  start=number;
	  label=ordstring;
	  fmtname="ordinal_fmt";
	  type="N";
	  output;
  end;


	keep start label fmtname type;
run;

proc format cntlin=ordinal_format;
run;

data want;
do i =1 to 100;
	j=i;
	output;
end;

format j ordinal_fmt.;
run;

@sivastat08 wrote:

Hi I want to create "July 2nd Week" in new column,Below is the sample code i tried but i stuck in adding ('th' 'st') dynamically  or is there any alternative way to create please help me on this .

 

data want;
format Today date9. month $3. Week $3. Cat $10.;
Today=today();
month=put(month(Today),monname3.);
Week=put(WEEKDAY(Today),weekday.);
Cat= put(catx(' ',month,Week,'Week'),$10.);
run;

 

Thanks,

Siva


 

View solution in original post

2 REPLIES 2
Reeza
Super User

I have an ordinal format here you can use:

 

https://gist.github.com/statgeek/6c885f6aea18dbd6caf8

 

data ordinal_format;
	length label $8.;
  do number=1 to 300;
    numstring = put(number, 16. -l);
    if prxmatch('/(?<!1)1\s*$/',numstring) then ending='st';
    else if prxmatch('/(?<!1)2\s*$/',numstring) then ending='nd';
    else if prxmatch('/(?<!1)3\s*$/',numstring) then ending='rd';
    else ending = 'th';
    ordstring =catt(numstring, ending);

	  start=number;
	  label=ordstring;
	  fmtname="ordinal_fmt";
	  type="N";
	  output;
  end;


	keep start label fmtname type;
run;

proc format cntlin=ordinal_format;
run;

data want;
do i =1 to 100;
	j=i;
	output;
end;

format j ordinal_fmt.;
run;

@sivastat08 wrote:

Hi I want to create "July 2nd Week" in new column,Below is the sample code i tried but i stuck in adding ('th' 'st') dynamically  or is there any alternative way to create please help me on this .

 

data want;
format Today date9. month $3. Week $3. Cat $10.;
Today=today();
month=put(month(Today),monname3.);
Week=put(WEEKDAY(Today),weekday.);
Cat= put(catx(' ',month,Week,'Week'),$10.);
run;

 

Thanks,

Siva


 

Kurt_Bremser
Super User

Create a custom format:

proc format;
value myxx
  1 = '1st'
  2 = '2nd'
  3 = '3rd'
  4 = '4th'
  5 = '5th'
  6 = '6th'
;
run;

This should suffice, as a month can't stretch over more than six weeks.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 1145 views
  • 2 likes
  • 3 in conversation