DATA Step, Macro, Functions and more

Help with if/else statement

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Help with if/else statement

[ Edited ]

Trying to add a functionalty of writing 0 Count if there are no observations. However it fails on the second else if statement and runs if the else if is commented away....

 

 

Proc freq data=reason ; table remove /out=total_rød_w missing nocol nocum norow nosparse ;


;
run;
Proc datasets nolist;
     contents DATA=work.total_rød_w out=temp(Keep=NOBS) noprint;
run;
Data check;
     Set temp total_rød_w;
     If NOBS = 0 Then do;
			CALL SYMPUT ('Count',PUT(0,10.0));
	 else if NOBS = 1 Then do;
	 		CALL SYMPUT ('Count',PUT(Count,10.0));
	 end;
Run;

Accepted Solutions
Solution
‎11-07-2016 07:12 AM
Super User
Posts: 5,513

Re: Help with if/else statement

If that's the sole objective, you can simplify the problem considerably:

 

data _null_;

call symputx('count', '0');

set total_rod_w nobs=_nobs_;

call symputx('count', _nobs_);

stop;

run;

 

If you actually want the same DATA step to write the report when there are 0 observations, slight modifications can make that happen.

View solution in original post


All Replies
Super User
Posts: 7,829

Re: Help with if/else statement

a) you use nobs, but I guess it is never defined, because there is no nobs= option in the set statement.

b) your first then do misses its corresponding end:

data check;
set temp total_red_w; * changed because of special character not allowed in SAS name;
if NOBS = 0
then do;
  call symput ('count',put(0,10.0));
end; * this is missing in your code;
else if NOBS = 1
then do;
  call symput ('count',put(Count,10.0));
end;
run;

For further help, post the log of the data step.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎11-07-2016 07:12 AM
Super User
Posts: 5,513

Re: Help with if/else statement

If that's the sole objective, you can simplify the problem considerably:

 

data _null_;

call symputx('count', '0');

set total_rod_w nobs=_nobs_;

call symputx('count', _nobs_);

stop;

run;

 

If you actually want the same DATA step to write the report when there are 0 observations, slight modifications can make that happen.

Contributor
Posts: 40

Re: Help with if/else statement

Posted in reply to Astounding

I just changed it a bit:

 

data _null_;
call symputx('count', '0');
set pande_total_rød_w nobs=_nobs_;
call symputx('count', COUNT);
stop;
run;

looks like a top soloution

Super User
Posts: 5,513

Re: Help with if/else statement

You can do something like that, but you have to use the right variable name.  If you want to use COUNT as the variable name in CALL SYMPUTX, you would have to code

 

set have nobs=count;

Super User
Super User
Posts: 7,976

Re: Help with if/else statement

Post exactly what you are doing, trying to do.  I don't see why you wouldn't just access the sashelp.vtable metadata information directly:

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="TOTAL_ROD_W"));
  call symput('Count',nobs);
run;

Not that I am entirely sure why you would want to put number of observations into a macro variable in the first place, that sounds like your whole process is a bit off.

Contributor
Posts: 40

Re: Help with if/else statement

[ Edited ]

I am using that number in an email report

Super User
Super User
Posts: 7,976

Re: Help with if/else statement

In which case drop the whole macro var bit and just do:

filename mymail ...;
data _null_;
  set set pande_total_rød_w nobs=_nobs_;
  file mymail;
  if _n_=1 then do;
    put "Number of observations: " _nobs_;
  end;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 253 views
  • 0 likes
  • 4 in conversation