BookmarkSubscribeRSS Feed
null
Fluorite | Level 6

Hi, I met one log issue from below code when using the macro call, would you please help? Thanks!!!

 

proc format;
value $__mth
'01' = 'JAN'
'02' = 'FEB'
'03' = 'MAR'
'04' = 'APR'
'05' = 'MAY'
'06' = 'JUN'
'07' = 'JUL'
'08' = 'AUG'
'09' = 'SEP'
'10' = 'OCT'
'11' = 'NOV'
'12' = 'DEC'
' ' = 'UNK'
;
value $__yr
' ' = 'UNK'
;
run;

 

*dtc may be like 2003-12-15T13:15:17;

%macro date(dtc=); * display as DDMMMYYYY;

length yy mm dd $5 dtc date $19;
dtc =%scan(&dtc.,1,T);
/* dtc =&dtc.;*/

if length(dtc)=10 and index(dtc,'--')=0 then date=strip(put(input(dtc,yymmdd10.),date9.));
else if length(dtc)=4 then date=strip(substr(dtc,1,4));
else if length(dtc)=9 then do; * 2003---15;
yy=substr(dtc,1,4);
mm=' ';
dd=substr(dtc,8,2);
date=strip(dd)||strip(put(mm,$__mth.))||strip(yy);
end;
else if length(dtc)=7 then do; * --12-15;
yy=' ';
mm=substr(dtc,3,2);
dd=substr(dtc,6,2);
date=strip(dd)||strip(put(mm,$__mth.))||strip(put(yy,$__yr.));
end;
drop yy mm dd ;
%mend date;

 

data raw;

HOSPSDTC='';output;

run;

 

data final;
length col4 $200;
set raw;
%date(dtc=HOSPSDTC);
col4=date;
run;

 

I don't understand why there's one log issue that "NOTE: Variable HOSPSD is uninitialized." when using the macro call %date.

I think it's related to the %scan function. Is there anything wrong?

 

the variable is HOSPSDTC, but the log issue is regarding to  HOSPSD... It's weird...

 

 

5 REPLIES 5
PaigeMiller
Diamond | Level 26

@null wrote:

data raw;

HOSPSDTC='';output;

run;


The above won't work, you should really make an effort to show us working code without typos.

 

data final;
length col4 $200;
set raw;
%date(dtc=HOSPSDTC);
col4=date;
run;

 

What is HOSPSDTC? You can't use a DATA step variable here, you'd have to use a macro variable in %date(), such as 

 

%date(dtc=&hospsdtc)

but no such macro variable has been defined in your program, so even if you put the & in front of HOSPSDTC, it sill won't work.

 

So, hard to say. But when you get errors in the log, SHOW US THE LOG. Use OPTIONS MPRINT; as the first line in your code, run it again, and show us the log. Include the entire sequence of code, and NOTES, WARNINGS and ERRORs. Do not chop anything out of the log. When providing the log, it is critical that you maintain the formatting of the log so we can see it exactly as SAS showed it to you, making it easier for us to use. To maintain the formatting of the log, click on the </> icon and paste the log as text into the window that appears. DO NOT SKIP THIS STEP.

 

--
Paige Miller
Quentin
Super User

At the top of the macro you use the %scan macro function:

dtc =%scan(&dtc.,1,T);

But it looks to me like your goal is the parse a data step variable, the name of which passed in the macro variable DTC.  Assuming that is correct, since you are scanning a data step variable, you would use the data step scan function, not the macro scan function:

dtc =scan(&dtc.,1,'T');

And note that the 'T' has to be in quotes for the SCAN function, because you are passing the literal value the character 'T' to be used as a delimiter.

 

If you make that change, I think your macro will work as you intend.

 

That said, from a quick look it looks like you're trying to read a character date-time string, and convert it to a character date string.  SAS provides built in informats and formats for these sorts of conversions, so you typically don't have to build your own parsing routine.  I think for the conversion you're doing, you could just code:

 

data raw;
  HOSPSDTC='2003-12-15T13:15:17';
  date=put(input(HOSPSDTC,B8601DT.),dtdate9.) ;
  put (HospsDTC date)(=) ;
run;

 

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
PaigeMiller
Diamond | Level 26

@Quentin wrote:

 

That said, from a quick look it looks like you're trying to read a character date-time string, and convert it to a character date string.  SAS provides built in informats and formats for these sorts of conversions, so you typically don't have to build your own parsing routine.  I think for the conversion you're doing, you could just code:

 

data raw;
  HOSPSDTC='2003-12-15T13:15:17';
  date=put(input(HOSPSDTC,B8601DT.),dtdate9.) ;
  put (HospsDTC date)(=) ;
run;

 


This is great, @Quentin , I didn't try to go through the code to figure out what this macro was trying to do, but your efforts here result in a great simplification of the code and logic.

--
Paige Miller
Kurt_Bremser
Super User

When you run your macro with parameter HOSPSDTC, this

%scan(&dtc.,1,T)

resolves to 

%scan(HOSPSDTC,1,T)

which means "dissect the text HOSPSDTC with the letter T as delimiter, and give me the first part".

So the whole statement resolves to

dtc = HOSPSD;

You use the macro function on a name of a data step variable. Since you want to deal with the contents of the variable, use the data step function SCAN.

Kurt_Bremser
Super User

PS values like "2003-12-15T13:15:17" are read into SAS datetime values by using the E8601DT19. informat, as they are compliant to the extended ISO 8601 standard. Then you either extract the date with the DATEPART function,  or use the DTDATE9. format to display only the date.

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!

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