06-18-2015 05:58 PM
sas data and the number of observation
If My data toto does not have any observation.
The number of observation is nbObs=0 ? or nbObs=
This will depend of sas 9.2 or sas 9.3 or sas 9.4
06-18-2015 08:39 PM
Sorry i could not follow your question. Could you please explain it with some more details.
Is it something about to get the number of obs count in a macro variable.
06-20-2015 03:37 AM
Thank you for your answers
I am asking a question. when i write this code, i get nobs=
In sas vers 9.2,
06-20-2015 03:43 AM
By the code your provided, you are trying to create a macro variable. As you know the macro variable take character values. i mean to say the macro variables consider any data as character (like a string). So when there is no data or zero observation, the macro variable created will have missing values as there are zero records. Hence the resolved macro variable has missing values.
In any version of sas you will get the same result.
06-20-2015 04:22 AM
The reason is: Iteration of the data step stops with the set statement as there are no data.
If you would code as below then the call symput() statement would get executed once, and _N_ would be set to '1' as it's the first iteration of the data step.
set test nobs=nobs;
I believe what you really want is as below:
if _n_=1 then call symputx('nobs',nobs);
set test nobs=nobs;
This will execute "call symputx()" also for cases with zero observations - but executes it only once if there are many observations - and it uses data set option "nobs" which contains the number of observations in a data set (which is also what you see when using Proc Contents). This will give you the result you're after.
06-20-2015 08:02 AM
06-21-2015 10:23 AM
when i write this test :
create table test1 as
select * from test where v >100; quit;
In sas version 9.4
I get :WARNING: Apparent symbolic reference NOBS not resolved.
but in sas version 9.2, i get this :
That's why , I ask, if this depends of the sas version.
06-21-2015 12:39 PM
That code will work the same in SAS version 9.4, 9.3, 9.2 .. 8.2 ... 6.12 ... . back to whenever they created the call symput() function.
The different output you are seeing is that in one session the macro variable NOBS had been created already by some code you have not listed and in the other it had not.
If you want to create a macro variable from the automatic variable _N_ and have it work even when the input dataset(s) are empty then you must place the CALL SYMPUT() function BEFORE the SET (or merge or input or ...) statement.
call symputX('NOBS',_n_ - 1);
set mydata ;
06-21-2015 02:20 PM
Produce the log.
data empty; stop; run;
data _null_; set empty ; call symput('nobs',_n_); run;
>sas612 test1 -log test1_sas612.log
>sas82 test1 -log test1_sas82.log
>sas92 test1 -log test1_sas92.log
>sas94 test1 -log test1_sas94.log
>grep ^WARNING test1_*.log
test1_sas612.log:WARNING: Apparent symbolic reference NOBS not resolved.
test1_sas82.log:WARNING: Apparent symbolic reference NOBS not resolved.
test1_sas92.log:WARNING: Apparent symbolic reference NOBS not resolved.
test1_sas94.log:WARNING: Apparent symbolic reference NOBS not resolved.
06-21-2015 03:00 PM
So the answer to your original question is that CALL SYMPUT() does not change the macro variable value if it does not run. You will see similar things when trying to create macro variables using PROC SQL INTO clause. So what you need to do is use more defensive coding techniques. Initialize your variables.
.... run some code that might change the macro variable ...
%if (&nobs > 0) %then %do;
06-22-2015 04:40 PM
Thank you tom for your message.
You're wright, I have the same warning in the sas 9.2
My matter was that:
At the begining of my programme, i write :
06-18-2015 09:02 PM
Are you asking a question or do you make a statement? Assuming what the question could be: In my understanding the row count is a property of the SAS data file and so what you get back depends on the version of the file and not of SAS reading the file.
I believe it has always been a '0' for an empty SAS file. Below an example for a SAS version 8 file.
libname test v8 'c:\temp';
put 'Number of observations: ' nobs;
set test.test nobs=nobs;