BookmarkSubscribeRSS Feed
Pooja98
Fluorite | Level 6

data friday;
target=today();
do n=1 to 5 while(date < NWKDOM(5, 6, month(target),year(target)));
date=NWKDOM(n, 6, month(target),year(target));
output;
end;
format target date date9.;
run;

 

When I run the above code, i got the output as:

07Jan2022

14Jan2022

21Jan2022

28Jan2022

 

BUT,

 

%macro test;
%let target=today();
%do n=1 %to 5 while(date <NWKDOM(5, 6, month(target),year(target)));
%let date= NWKDOM(n,6, month(target),year(target));
output;
%end;
%mend test;

 

When i run the above code with same condition, I got the output as:

07Jan2022

 

The output I want is:

07Jan2022

14Jan2022

21Jan2022

28Jan2022

 

Can someone help me to overcome this situation!!

 

I want 4 macro values in a single macro variable.

 

TIA,

Joe

6 REPLIES 6
Kurt_Bremser
Super User

Run your macro after issuing

options mlogic mprint symbolgen;

and post the log into a window opened with this button:

Bildschirmfoto 2020-04-07 um 08.32.59.jpg

so we can point out the many issues.

Pooja98
Fluorite | Level 6

441 options mlogic mprint symbolgen;
442
443 %macro test;
444 %let target=today();
445 %do n=1 %to 5 while(date <NWKDOM(5, 6, month(target),year(target)));
446 %let date= NWKDOM(n,6, month(target),year(target));
447 output;
448 %end;
449 %mend test;
450
451 %put &date;
SYMBOLGEN: Macro variable DATE resolves to 07JAN2022
07JAN2022

 

 

This is the log i got

Kurt_Bremser
Super User

You never actually call your macro, so the value displayed from the macro variable comes from and earlier code submit.

 

But let's go to the macro code itself (I am now on my main desktop, not on my tablet):

%macro test;
%let target=today(); /* to get the RESULT(!) of a function in macro language, you have to wrap it into the %SYSFUNC macro function */
%do n=1 %to 5 while(date <NWKDOM(5, 6, month(target),year(target)));
/* The keyword WHILE would have to be %WHILE to be recognized by the macro processor,
   but you cannot combine %TO with %WHILE in macro language */
/* You also must call macro variables by using ampersands, otherwise the macro variable names are only text */
/* And you also must wrap all function calls in %SYSFUNC */
%let date= NWKDOM(n,6, month(target),year(target));
/* Use ampersands and %SYSFUNC */
output; /* this would simply create a sequence of OUTPUT statements wherever the macro is called,
   the validity depends on where the macro is called */
%end;
%mend test;

Your data step creates a dataset, which cannot be done from a macro (except with VERY advanced macro coding techniques, from which you are lightyears right now). What should the macro do?

Pooja98
Fluorite | Level 6
Hi,

I have four folders (name: 20220107, 20220114, 20220121, 20220128) each folder contain some observation related to the folder date. Data's are in the text format. I want to import all weekly data into SAS when i run the import code only once.
Kurt_Bremser
Super User
%macro import_all;
%let date = %sysfunc(inputn(20220107,yymmdd8.));
%do %while (&date. le %sysfunc(inputn(20220128,yymmdd8.)));
  %let folder = %sysfunc(putn(&date.,yymmddn8.));

  data ds&folder.;
  infile "/path/&folder./data.txt" /* other options */
  input /* list of variables */;
  run;

  %let date = %eval(&date.+7);
%end;
%mend;
%import_all
Tom
Super User Tom
Super User

Create a program (not a macro) that reads the files from one of the folders.

Perhaps your program looks something like this:

data want;
  infile '/toplevel/20220107/alldata.txt' dsd firstobs=2 truncover;
  input id date :yymmdd. var1-var10;
run;

Now change the date part of the folder name to macro variable reference. Make sure to use double quotes instead of single quotes so the macro variable reference will work.

%let datestr=20220107;
data want;
  infile "/toplevel/&datestr./alldata.txt" dsd firstobs=2 truncover;
  input id date :yymmdd. var1-var10;
run;

Once you get that working now convert it to a macro that uses that macro variable as a parameter. And try calling it with the same date string.

%macro readone(datestr);
data want;
  infile "/toplevel/&datestr./alldata.txt" dsd firstobs=2 truncover;
  input id date :yymmdd. var1-var10;
run;
%mend;
%readone(datestr=20220107)

Now you can use the dataset you have with the list of dates to call that macro once for each date.

Say you had a dataset named FRIDAYS with a variable named DATE that had a SAS date (numeric variable with number of days since 1960).  Use PUT() to convert the date into the digit string your macro needs. Use CATS() to generate the macro call. Use CALL EXECUTE() to push the call onto the stack to run after the data step finishes.  Use the %NRSTR() macro function to delay running of the macro until the command is pull back off the stack to be executed.

data _null_;
  set fridays;
  call execute(cats('%nrstr(%readone)(datestr=',put(date,yymmddn8.),')'));
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1351 views
  • 2 likes
  • 3 in conversation