DATA Step, Macro, Functions and more

Date from infile

Reply
Super Contributor
Posts: 647

Date from infile

I have a macro variable Filename that resolves to:
S:\Per\IMS\SAS\INFILES\Jan 2010 ZUD.txt

How to create macro variables:
datamon and currmon that takes values from the file.
datamon resolves to 201001
currmon resolves to 2010-01-01
Valued Guide
Posts: 632

Re: Date from infile

Knowing nothing of the structure of the incoming file, it is a bit hard to give a definitive response. Generally in flat files the INFORMAT is used to read date form data into SAS dates. The family of ANYDT informats is the most flexible when the column is not constant, but there are many others. For the two that you mentioned look at YYMMN and YYMMDD .
SAS Super FREQ
Posts: 8,743

Re: Date from infile

If I understand you correctly, you want to take the strings for MONTH and YEAR out of the &FILENAME macro variable and from those strings you want to create &DATAMON and &CURRMON??? Is that correct?

&FILENAME resolves to S:\Per\IMS\SAS\INFILES\Jan 2010 ZUD.txt
from &FILENAME, &DATAMON would resolve to 201001 (a text string) and
&CURRMON would resolve to 2010-01-01 (a text string, where the day is always 01)

I wonder because you said you wanted to take "values from the file" and not from the &FILENAME macro variable. Can you clarify?

cynthia
Super Contributor
Posts: 647

Re: Date from infile

from &filename
Super Contributor
Super Contributor
Posts: 3,174

Re: Date from infile

If you are macro language challenged (with using %SYSFUNC and associated SAS CALL functions), then you could run a SAS DATA step using the macro variable string &FILENAME, parse it using SUBSTR(...) and then use the PUT(...) function with the desired output format. The last statement would be CALL SYMPUT functions to generate your new SAS macro variables.

Otherwise, using SAS macro language, you can use %SUBSTR(...) to parse your macro variable, then use %SYSFUNC(...) to translate the month-year data-string, first to a SAS DATE variable, then back to a SAS output-format string and macro variables, as needed.

Suggest you take the challenge one step at a time to get the data-string parsed, then converted to a SAS DATE, then re-formatted to your macro variables in the format needed.

Scott Barry
SBBWorks, Inc.
Super Contributor
Super Contributor
Posts: 365

Re: Date from infile

Hello SASPhile,

It could look something like this:
[pre]
%let m=S:\Per\IMS\SAS\INFILES\Jan 2010 ZUD.txt;
%let mt=%SUBSTR(%SCAN(&m,-1,%STR(\)),1,3);
%let yr=%SUBSTR(%SCAN(&m,-1,%STR(\)),5,4);
%put mt=&mt yr=&yr;
data _null_;
length m $2;
date="01&mt.&yr."d;
format date date7.;
m=put(MONTH(date),2.);
if month(date) < 10 then m="0"||m;
call symputx('mon',m);
run;
%put mon=&mon yr=&yr;
%let datamon=%SYSEVALF(&yr*100+&mon);
%let currmon=&yr.-&mon.-01;
%put datamon=&datamon currmon=&currmon;
[/pre]
Sincerely,
SPR
SAS Super FREQ
Posts: 8,743

Re: Date from infile

Hi:
You can also do this without using a DATA step program because %SYSFUNC allows you to invoke any of the other functions you need. See the SAS log below. I did intermediate %PUT statements to illustrate what macro variables were being created. Certainly, in a production system, some of the %LET statements are not needed and the %PUT statements are not needed. They are just there for illustration purposes.

cynthia
[pre]
2005 %let fname = %str(S:\Per\IMS\SAS\INFILES\Jan 2010 ZUD.txt);
2006 %let datestr = %scan(&fname,6,:\.);
2007 %put fname=&fname datepiece=&datestr;
fname=S:\Per\IMS\SAS\INFILES\Jan 2010 ZUD.txt datepiece=Jan 2010 ZUD
2008
2009 %let cmon = %scan(&datestr,1,%str( ));
2010 %let cyr = %scan(&datestr,2,%str( ));
2011 %put cmon=&cmon cyr=&cyr;
cmon=Jan cyr=2010
2012
2013 %let internal = %sysfunc(inputn(01&cmon.&cyr,date9.));
2014 %let currmon = %sysfunc(inputn(01&cmon.&cyr,date9.),yymmdd10.);
2015 %put internal sas date=&internal currmon=&currmon;
internal sas date=18263 currmon=2010-01-01
2016
2017 %let mm = %sysfunc(month(&internal),z2.);
2018 %let datamon = &cyr.&mm;
2019 %put mm=&mm cyr=&cyr datamon=&yrmm;
mm=01 cyr=2010 datamon=201001
[/pre]
Ask a Question
Discussion stats
  • 6 replies
  • 145 views
  • 0 likes
  • 5 in conversation