sas data and the number of observation

Reply
Super Contributor
Posts: 371

sas data and the number of observation

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

Thank you

Super User
Posts: 1,117

Re: sas data and the number of observation

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.

Thanks,
Jag
Super Contributor
Posts: 371

Re: sas data and the number of observation

Thank you for your answers

I am asking a question. when i write this code, i get  nobs=

In sas vers 9.2,

data test;

  stop;

  set sashelp.class;

run;

data _null_;

set test;

call symput('nobs',_N_);

run;

%put nobs=&nobs;

I get

nobs=

Super User
Posts: 1,117

Re: sas data and the number of observation

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.

Thanks,
Jag
Respected Advisor
Posts: 3,837

Re: sas data and the number of observation

Actually - and in disagreement with - when you run your code what you will get is: WARNING: Apparent symbolic reference NOBS not resolved.

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.

data _null_;

  call symput('nobs',_N_);

  set test nobs=nobs;

run;

I believe what you really want is as below:

data _null_;

  if _n_=1 then call symputx('nobs',nobs);

  set test nobs=nobs;

run;

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.

Super User
Posts: 1,117

Re: sas data and the number of observation

Thank you for the correction, I totally agree with you , when you try to create a macro variable with null values, the variable will not be created and will throw the warning.  I should have tested that before responding.

, Patrick has the right explanation for our query.

Thanks,
Jag
Super Contributor
Posts: 371

Re: sas data and the number of observation

when i write this test :

data test;

input v;

cards;

12

13

;

run;

proc sql;

create table test1 as

  select * from test where v >100; quit;

data _null_;

  set test1;

  call symput('nobs',_N_);

run;

%put nobs=&nobs;

In sas version 9.4

I get :WARNING: Apparent symbolic reference NOBS not resolved.

but in sas version 9.2, i get this :

nobs=

That's why , I ask, if this depends of the sas version.

Thank you

Super User
Super User
Posts: 6,364

Re: sas data and the number of observation

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.

data _null_;

    call symputX('NOBS',_n_ - 1);

    set mydata ;

run;

Super Contributor
Posts: 371

Re: sas data and the number of observation

Thank you Tom for your message.

No, it was not created in any session before.

Super User
Super User
Posts: 6,364

Re: sas data and the number of observation

Produce the log.

>more test1.sas    

data empty; stop; run;

data _null_; set empty ; call symput('nobs',_n_); run;

%put NOBS=&nobs;

>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.

>

Super Contributor
Posts: 371

Re: sas data and the number of observation

Thank you.

I will check again.

How can I check, if my sas version has not a matter ?

Super User
Super User
Posts: 6,364

Re: sas data and the number of observation

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.

%let nobs=0;

....  run some code that might change the macro variable ...

%if (&nobs > 0) %then %do;

....

Super Contributor
Posts: 371

Re: sas data and the number of observation

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 :

%global nobs;

Respected Advisor
Posts: 3,837

Re: sas data and the number of observation

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';

data test.test;

  stop;

  set sashelp.class;

run;

data _null_;

  put 'Number of observations: ' nobs;

  set test.test nobs=nobs;

run;

Super Contributor
Posts: 371

Re: sas data and the number of observation

Thank you for your message.

Yes, i am asking a question.

Ask a Question
Discussion stats
  • 15 replies
  • 643 views
  • 7 likes
  • 4 in conversation