Help using Base SAS procedures

Better way to do

Accepted Solution Solved
Reply
Regular Contributor
Posts: 168
Accepted Solution

Better way to do

Hi All,

I want to create macro variable, Any better way to do.

For example

My macro variable data look like :20oct1997 , but i want to create a new macro variable which look like : 20 OCT 1997

this my approach:

%let dt=20oct1997;

data _null_;

sdt="%upcase(&dt)";

newdt=cat(substr(sdt,1,2),' ',substr(sdt,3,3),' ',substr(sdt,length(sdt)-3,4));

call symputx("newdt",newdt);

run;

%put &newdt;

Thanks

Sam


Accepted Solutions
Solution
‎02-17-2015 03:56 PM
Super User
Super User
Posts: 6,498

Re: Better way to do

Perhaps it is easier to see if you don't think about it as a date?

%let old=123456789;

%let new=%substr(&old,1,2) %substr(&old,3,3) %substr(&old,6);

%put "&new";

"12 345 6789"

%let old=20oct1997;

%let new=%upcase(%substr(&old,1,2) %substr(&old,3,3) %substr(&old,6));

%put "&new";

"20 OCT 1997"

View solution in original post


All Replies
Solution
‎02-17-2015 03:56 PM
Super User
Super User
Posts: 6,498

Re: Better way to do

Perhaps it is easier to see if you don't think about it as a date?

%let old=123456789;

%let new=%substr(&old,1,2) %substr(&old,3,3) %substr(&old,6);

%put "&new";

"12 345 6789"

%let old=20oct1997;

%let new=%upcase(%substr(&old,1,2) %substr(&old,3,3) %substr(&old,6));

%put "&new";

"20 OCT 1997"

Trusted Advisor
Posts: 1,300

Re: Better way to do

If you are always performing this on dates, then you can use a custom picture format to display the dates the way you want.

proc format;

   picture spaced_dt

      (default=11)

         other = '%0d %b %Y'

      (datatype=date)

      ;

   run;

%let dt = 20oct1997;

%put %sysfunc(putn("&dt."d, spaced_dt.));

Respected Advisor
Posts: 3,124

Re: Better way to do

Sometimes the question becomes why you need to do that? AND how did the date get into the first macro variable? AND will we need Macro variable at all?

Super Contributor
Posts: 275

Re: Better way to do

%let dt=20oct1997;

%let newdt=%sysfunc(prxchange(s/(\d{2})(\w+)(\d{4})/$1 \U$2 $3/i,-1,&dt));

%put &newdt;

Regular Contributor
Posts: 168

Re: Better way to do

Thank you All,

I agree with you Hai.kuo. while i am practicing  with macro variables with date character variable. i got a question if we need to keep space between the date, how should i achieve it. i tried one approach i am looking for better one.. so i posted on forum. i want to learn different approach , from SAS Guru 's on this forum.

Thanks

Sam

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 250 views
  • 7 likes
  • 5 in conversation