SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
harrylui
Obsidian | Level 7

hi all,

 

can not run this script properly on the data step.

can anyone tell me what do i miss?

 

%let cnt=0;
%do i = 0 %to 10 ;
%let date=%sysfunc(putn(%sysfunc(intnx(day,'12mar2021'd,%eval(&i*-1),s)),DDMMYY10.));
%put &date.;


data check;
call symputx(haha,&date.);
datalines;

%end;

 

Regards,

Harry

10 REPLIES 10
japelin
Rhodochrosite | Level 12

First, I don't know what you want to do.

I'm not an esper, so I can't give you advice on this.


What do you see in the log when you run this code?Please check the log.

 

 

 

PaigeMiller
Diamond | Level 26

There are too many problems here. The first step in writing any macro is to create code that actually works without macros and without macro variables, for two cases, for example when  i=0 and  i=1. You haven't done this and your code doesn't meet that criterion. If you can write such code without macros and without macro variables, then it is relatively easy to turn it into a working macro. If you can't get it to work without macros and without macro variables, then you will never get it to work with macros and with macro variables first.

 

So that's the first step. When we ask people to create working SAS code without macros and without macro variables, most people never do this, and ignore the request. That is not the way to learn, and that is not the way to get results. So please, take our advice. Get the code to work without macros and without macro variables as a first step, and then we can show you how to make this a macro that works.

 

As far as the code presented:

 

you don't have a macro, and %DO will not run outside of a macro. Once you fix that, there are many other problems.

 

For your own debugging, put the following as the first line of your code

 

options mprint symolgen mlogic;

 

then re-run the program, look at the LOG, and see if you can figure out what the problems are. Fix as many as you can, and if you get stuck, show us the CODE and the LOG. Paste the LOG as text into the Window that appears when you click on the </> icon. Do not show us the LOG any other way. But again, as I said above, if your code doesn't work without macros and without macro variables, it will never work with macros and with macro variables.

 

--
Paige Miller
ballardw
Super User

Hint:

If you do not want to actually create a data set and just execute some lines of code, such as your Call Symputx, use a data _null_. Example:

 

data _null_;
   call symputx(haha,&date.);
run;

 

I am not claiming the above will run, but something like that is better than creating a data set with no observations or variables.

Kurt_Bremser
Super User

Look at this:

data check;
call symputx(haha,&date.);

The first argument to SYMPUTX must be a string expression, but you supply a variable (haha) that is not defined beforehand (neither read from a file or a dataset, nor assigned a value). The data step compiler creates the variable as numeric, and it will always be missing (look for the "uninitialized" NOTE in the log). A missing numeric value is represented with a single dot, which is not valid for macro variable names.

Tom
Super User Tom
Super User

Please explain what you are trying to do.

If you want to assign the value of the macro variable DATE to a new macro variable named HAHA then just use a %LET statement.

%let haha=&date;
japelin
Rhodochrosite | Level 12

 

If you just want to create a dataset with date, you can do so with the following.

data check(drop=i);
  do i = 0 to 10 ;
    date=put(intnx('day','12mar2021'd,(i*-1),'s'),DDMMYY10.);
    call symputx(cats("haha",i),date);/* for example, this statement saves all date to macro variabled haha0-10 */
    output;
  end;
run;
harrylui
Obsidian | Level 7

 

%do i = 0 %to 10 ;
%let date=%sysfunc(putn(%sysfunc(intnx(day,'12mar2021'd,%eval(&i*-1),s)),DDMMYY10.));
%put &date.;

%end;

 

 

Actually this script stand alone can run Smoothly and below is the log:

 

6 %do i = 0 %to 10 ;
7 %let date=%sysfunc(putn(%sysfunc(intnx(day,'12mar2021'd,%eval(&i*-1),s)),DDMMYY10.));
8 %put &date.;
9
10 %end;
11
12/03/2021
11/03/2021
10/03/2021
09/03/2021
08/03/2021
07/03/2021
06/03/2021
05/03/2021
04/03/2021
03/03/2021
02/03/2021
12 quit; run;
13 ODS _ALL_ CLOSE;

 

 

i just want to create a dataset for it. 

is it good to use a symputx to create it 

japelin
Rhodochrosite | Level 12

If you just want to create a dataset, there is no need to use macros or call symputx.

Use this code.

data check(drop=i);
  do i = 0 to 10 ;
    date=put(intnx('day','12mar2021'd,(i*-1),'s'),DDMMYY10.);
    output;
  end;
run;
Kurt_Bremser
Super User

Positively no need for any macro code. A simple do loop will do it:

data want;
format date ddmmyy10.;
do i = 0 to -10 by -1;
  date = intnx('day','12mar2021'd,i,'s');
  output;
end;
run;
Tom
Super User Tom
Super User

Is this 

&i*-1

Just a convoluted way to write

-&i

?

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 1857 views
  • 6 likes
  • 6 in conversation