@Babloo wrote:
Despite executing the following code (only the below code and any step prior to it) multiple times, I could see only the below NOTE. It's not adding any observation.
... NOTE: The data set WORK.WANT has been updated. There were 0 observations rewritten, 1 observations added and 0 observations deleted.
Why do you think it's "not adding any observation" when the log says "1 observations added
" and the PROC PRINT step (which you omitted from my code) confirms that?
data want;
set have end=eof;
output;
if eof
then do;
count + 1;
output;
end;
run;
I ran the code below and I'm seeing the Output as 0 Observation instead of 1 Observation.
Desired Output is 1 for the run_id if there is no Observation in the HAVE dataset. If any Observation then I want to add the run_id by 1.
data have;
input run_id;
datalines;
run;
data want;
set have nobs=nobservation nobs=n_obs;
if n_obs=0 then run_id=1;
else run_id+1;
run;
Log:
26 data have;
27 input run_id;
28 datalines;
NOTE: The data set WORK.HAVE has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
29 run;
30
31 data want;
32 set have nobs=nobservation nobs=n_obs;
33 if n_obs=0 then run_id=1;
34 else run_id+1;
35 run;
NOTE: There were 0 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 0 observations and 1 variables
I tried with your apporach like below as well and it is also not working.
26 data want;
27 set have end=eof;
28 output;
29 if eof
30 then do;
31 run_id+1;
32 output;
33 end;
34 run;
NOTE: There were 0 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 0 observations and 1 variables.
Yes, we need to take care for the empty dataset:
data want;
if _n_ = 1 and eof
then do;
run_id = 1;
output;
end;
set have end=eof;
output;
if eof
then do;
run_id + 1;
output;
end;
run;
Thanks, your code works well for 0 observation in the HAVE dataset. In case if I want to grow the run_id by 1 for each run, then how can we tweak the code.?
For example, code below works well if I've Observation in HAVE and the code which you posted works well for 0 Observation. Now if I want to combine this behaviour how can we do it? I regret if I didn't meant this objective in my IP.
27 data have;
28 input run_id;
29 datalines;
NOTE: The data set WORK.HAVE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
31 ;
32 run;
33
34
35
36
37 proc sql noprint;
38 select max(max(run_id)+1, 1) into :_newid
39 from want;
40 insert into want
41 set run_id=&_newid;
NOTE: 1 row was inserted into WORK.WANT.
42 quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
43
44 proc print data=want;
2 The SAS System Friday, September 13, 2019 11:02:00 AM
45 run;
NOTE: There were 9 observations read from the data set WORK.WANT.
Hi @Babloo,
Try this PROC SQL approach:
/* Assuming this already exists
data want;
stop;
run_id=.;
run;
*/
proc sql noprint;
select max(max(run_id)+1, 1) into :_newid
from want;
insert into want
set run_id=&_newid;
quit;
proc print data=want;
run;
If you run this code repeatedly, you'll see how dataset WANT is growing with RUN_ID=1, 2, 3, ...
Okay, then try this:
data want;
run_id=sum(n,1);
output;
stop;
set want nobs=n;
modify want;
run;
proc print data=want;
run;
Again, run the above code repeatedly (starting with the empty WANT dataset) to see the effect.
Despite executing the following code (only the below code and any step prior to it) multiple times, I could see only the below NOTE. It's not adding any observation.
26 data want;
27 run_id=sum(n,1);
28 output;
29 stop;
30 set want nobs=n;
31 modify want;
32 run;
NOTE: The data set WORK.WANT has been updated. There were 0 observations rewritten, 1 observations added and 0 observations
deleted.
@Babloo wrote:
Despite executing the following code (only the below code and any step prior to it) multiple times, I could see only the below NOTE. It's not adding any observation.
... NOTE: The data set WORK.WANT has been updated. There were 0 observations rewritten, 1 observations added and 0 observations deleted.
Why do you think it's "not adding any observation" when the log says "1 observations added
" and the PROC PRINT step (which you omitted from my code) confirms that?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.