BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Kirito1
Quartz | Level 8

I was working on a code where after working on dataset. I exported the file with filename as date and number of observation. The code was working awesome until I tested this code on a dataset with Zero observation: The output was not desirable. (SS attached) 

proc import datafile='/ABC_FOLDER/IMPORT/H.csv' 
out=CUSTAVG
DBMS=CSV
REPLACE;
run;


data Want; set CUSTAVG;
where 'Cust ID'n NE 1;
run;


%let PRDATE = Date();
data _null_;
   set Want nobs=c;
   call symputx('trans_n',c);
   call symputx('yesterday',put(intnx('day',&PRDATE.,0),date9.));
run;

proc export data=Want
outfile="/ABC_FOLDER/Outfile_file/TEST__&yesterday._&trans_n..csv"
REPLACE
DBMS=CSV;
run;

The output coming for 0 observations

Kirito1_0-1677492391813.png

Kirito1_1-1677492435149.png

I don't know how the code is extracting 7 records. 

Note: that this is working even for 1 observation correctly but not for 0(zero) observations.

(Also, I know this is not what a name nomenclature should be like but the user has requirement. So, Kindly, Cooperate)

Kindly, help. 

Your help is much appreciated. 

 

Thanks in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

The SAS log shows you that the .csv file has been created with zero observations. 

The value of 7 for &trans_n must have been set in a previous execution of your code. 

 

Make it a habit to always create a new SAS session for such testing. If you would have done so then the SAS log would have told you:

Patrick_0-1677498461236.png

 

If you change your code to below then things will work as desired.

data _null_;
  call symputx('trans_n',c);
  call symputx('yesterday',put(intnx('day',&PRDATE.,0),date9.));
  stop;
  set Want nobs=c;
run;

nobs=c gets set during compilation time, call symputx() gets only run during execution time.

As soon as SAS "hits" a set statement with a table that has zero rows it stops the iteration and no further statements will get executed.

If you have the set statement AFTER call symputx() then the statement executes also if the source table got zero observation. 

Because you only ever need call symputx() executed once using a stop command avoids repetition of call symputx() for every single row in the source table.

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

When you have errors in the log, we need to see the ENTIRE log for this code. As you have provided only slight bits of the log, we don't even know which step had the error. Always, from now, please show us the ENTIRE log.

--
Paige Miller
Patrick
Opal | Level 21

The SAS log shows you that the .csv file has been created with zero observations. 

The value of 7 for &trans_n must have been set in a previous execution of your code. 

 

Make it a habit to always create a new SAS session for such testing. If you would have done so then the SAS log would have told you:

Patrick_0-1677498461236.png

 

If you change your code to below then things will work as desired.

data _null_;
  call symputx('trans_n',c);
  call symputx('yesterday',put(intnx('day',&PRDATE.,0),date9.));
  stop;
  set Want nobs=c;
run;

nobs=c gets set during compilation time, call symputx() gets only run during execution time.

As soon as SAS "hits" a set statement with a table that has zero rows it stops the iteration and no further statements will get executed.

If you have the set statement AFTER call symputx() then the statement executes also if the source table got zero observation. 

Because you only ever need call symputx() executed once using a stop command avoids repetition of call symputx() for every single row in the source table.

Kirito1
Quartz | Level 8
The code you gave is getting the desired output but I am unable to understand what exactly is happening. Kindly, explain . Because I am unable to understand the summary you gave it kind of confused me. Much appreciated.
Kurt_Bremser
Super User

@Kirito1 wrote:
The code you gave is getting the desired output but I am unable to understand what exactly is happening. Kindly, explain . Because I am unable to understand the summary you gave it kind of confused me. Much appreciated.

The SET statement has two functions: at data step compile time, the metadata of the dataset is fetched and used to define the Program Data Vector (PDV), and other stuff like the NOBS= variable you use. During data step execution, it is also where the actual read of a new observation is done.

When the SET tries to read past the last observation, it terminates the data step. Since this happens immediately in the first iteration of a data step when a dataset contains zero observations, your original data step terminates before the CALL SYMPUT calls could execute. By moving the CALL SYMPUTs ahead of the SET, this is avoided

Kurt_Bremser
Super User
intnx(`day`,&prdate.,0)

returns the same value stored in &prdate, so the function call is not needed.

 

And I would never use a date formatted with DATE. in a filename. Use a fully numeric date in YMD order, formatted with YYMMDDN8. This automatically sorts chronologically, and makes the use of wildcards to pick years or months much easier.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 5 replies
  • 1079 views
  • 9 likes
  • 4 in conversation