BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Babloo
Rhodochrosite | Level 12
I have a situation where I need to increase the value by 1 to create a new variable(numeric) called 'count_execution' in one new dataset (want) if I execute any program from particular folder. Dataset 'want' is already created with 0 observation. So I want to insert value as 1 only for the first record in WANT dataset and for the subsequent records values should be dynamically inserted like 2, 3, 4, 5...

E.g.

Count_execution
1
2
3
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

@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?

View solution in original post

9 REPLIES 9
Babloo
Rhodochrosite | Level 12

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.
Kurt_Bremser
Super User

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;
Babloo
Rhodochrosite | Level 12

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.
FreelanceReinh
Jade | Level 19

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

Babloo
Rhodochrosite | Level 12
Thanks. Curious to know about data step method as well.
FreelanceReinh
Jade | Level 19

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.

Babloo
Rhodochrosite | Level 12

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.
FreelanceReinh
Jade | Level 19

@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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2259 views
  • 3 likes
  • 3 in conversation