BookmarkSubscribeRSS Feed
prad001
Obsidian | Level 7

Hi All,

 

I am trying to put the value of 'i' in a macro within a data step using call symput. When csv_name is resolving accurately with the value of i, then why not file_cnt ? Please let me know where did i go wrong or show me a way of how to rewrite this query more efficiently. Help appreciated..

 

Thanks alot.

 

 

%let num=100;

DATA new1;
do new=1 to 1000; output;end;
run;
proc sort data=new1;by new;run;
 
data new2;
set new1;
by new;
 
IF _n_=1 THEN do; 
  i=1;
j=0; 
END;
 
RETAIN i j;
 
IF j=100 then do;
i=i+j; 
j=0;
END;
 
********************************************************
In this block I am creating names for the csv dividing them into 100 records / csv
******************************************************;
 
 
IF NEW >= i and NEW <= (i+&num-1) THEN DO;
csv_name=i;
call symput('file_cnt',put(i,3.));
******************************************************
Below is the file_cnt which is not resolving as per the need
******************************************************;
file = "abc_&file_cnt._dt.csv";
END;
 
 
 
IF First.NEW THEN DO;
j=j+1;
csv=i;
ENd;
run;
2 REPLIES 2
Tom
Super User Tom
Super User

I cannot make sense of your logic, but your error with respect to macro variables is clear.

 

When the data step is compiled this statement is evaluated:

file = "abc_&file_cnt._dt.csv";

And turned into something like:

file = "abc_123_dt.csv";

Then later when the data step is running this line will execute

call symput('file_cnt',put(i,3.));

and set a value for FILE_CNT that you could use in the NEXT block of SAS code that you want to generate.

 

If you want the value of FILE to be based on the value of I then tell SAS to build it using I and not some macro variable.

file = cats('abc_',put(i,Z3.),'_dt.csv');
novinosrin
Tourmaline | Level 20

I can't make sense of the logic here. But methinks resolve is what you are after. is this what you are after by any chance?

 

file = resolve('abc_&file_cnt._dt.csv');

So, if yes

 



%let num=100;

DATA new1;
do new=1 to 1000; output;end;
run;
proc sort data=new1;by new;run;
 
data new2;
set new1;
by new;
 
IF _n_=1 THEN do; 
  i=1;
j=0; 
END;
 
RETAIN i j;
 
IF j=100 then do;
i=i+j; 
j=0;
END;
 
********************************************************
In this block I am creating names for the csv dividing them into 100 records / csv
******************************************************;
 
 
IF NEW >= i and NEW <= (i+&num-1) THEN DO;
csv_name=i;
call symputx('file_cnt',put(i,3.));
******************************************************
Below is the file_cnt which is not resolving as per the need
******************************************************;
file = resolve('abc_&file_cnt._dt.csv');
END;
 
 
 
IF First.NEW THEN DO;
j=j+1;
csv=i;
ENd;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 465 views
  • 2 likes
  • 3 in conversation