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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Tom
Super User Tom
Super User

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"

FriedEgg
SAS Employee

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.));

Haikuo
Onyx | Level 15

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?

slchen
Lapis Lazuli | Level 10

%let dt=20oct1997;

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

%put &newdt;

sam369
Obsidian | Level 7

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 808 views
  • 7 likes
  • 5 in conversation