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...
@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.
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;
@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.
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.