BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
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
6 REPLIES 6
ArtC
Rhodochrosite | Level 12
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 .
Cynthia_sas
SAS Super FREQ
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
SASPhile
Quartz | Level 8
from &filename
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
Cynthia_sas
SAS Super FREQ
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]

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!

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
  • 6 replies
  • 807 views
  • 0 likes
  • 5 in conversation