SAS Programming

DATA Step, Macro, Functions and more
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

Appericiate if someone of you help me tweak the code below to create missing value for ENITY_NM (character variable) if there is 0 obersvation in the Input dataset 'dis_entity_id'

 

752        /*extract and append ENTY_NMs based on Entity_ID*/
753        
754        data reqd_vars (drop=ENTITY_ID);
755        	set dis_entity_id end=last_record;
756        	by ENTITY_ID;
757        	length ENTY_NM $50;
758        	retain ENTY_NM;
759        	if first.ENTITY_ID then
760        	ENTY_NM=catx('/',ENTY_NM,put(ENTITY_ID, $ENTITY.));
761        	if last_record then
762        		output;
763        run;

NOTE: There were 0 observations read from the data set WORK.DIS_ENTITY_ID.
NOTE: The data set WORK.REQD_VARS has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds

Desired Output if the source dataset  'dis_entity_id' has 0 bbservation,

 

ENTY_NM

" "

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @Babloo  If you have 0 obs in your dataset and want an observation with missing values, here is one easy way

 

data want;
if 0 then set have;
call missing(of _all_);
output;
stop;
run;

View solution in original post

8 REPLIES 8
VinitvictorCorr
Quartz | Level 8
one suggestion, not sure if it would work but in the second if statement do the below.

if last_record then do;
enity_nm="";
output;
end;
Babloo
Rhodochrosite | Level 12

I tried and it didn't work as I got 0 observation

VinitvictorCorr
Quartz | Level 8
you might get the observations with missing character values

novinosrin
Tourmaline | Level 20

Hi @Babloo  If you have 0 obs in your dataset and want an observation with missing values, here is one easy way

 

data want;
if 0 then set have;
call missing(of _all_);
output;
stop;
run;
Babloo
Rhodochrosite | Level 12

I tried the code as follows and ended up with error. If there is Observation in the dataset then I want to get that value in 'vars' dataset.

 

2859       data vars;
2860       if 0 then set dis_entity_id;
2861       call missing(of _all_);
2862       else set dis_entity_id;
           ____
           160
ERROR 160-185: No matching IF-THEN clause.

2863       output;
2864       stop;
2865       run;

NOTE: The SAS System stopped processing this step because of errors.
novinosrin
Tourmaline | Level 20

Hi @Babloo  Oh that logic is solely for zero obs dataset and please do not confuse and modify the logic like you did. Please follow the code below and see if you understand. I have written some comments to help you.

 


%macro check_and_do;
/*Get the number of obs in macro var N*/
proc sql noprint;
select nobs into :n trimmed
from dictionary.tables 
where libname='WORK' and memname='HAVE';
quit;

%put &=n;/*N is the number of obs in HAVE*/
/*Check if N>0*/
%if &n>0 %then %do;
 data want;
 set have;
 run;
%else %do;
/*If n=0 then do the below*/
 data want;
 if 0 then set have;
 call missing(of _all);
 output;
 stop;
 run;
 %end;
%mend check_and_do;

%check_and_do
Babloo
Rhodochrosite | Level 12

Am I doing something wrong?

 

26         %macro check_and_do;
27         /*Get the number of obs in macro var N*/
28         proc sql noprint;
29         create table have as select nobs into :n
30         from sashelp.class;
31         quit;
32         
33         %put &=n;/*N is the number of obs in HAVE*/
34         /*Check if N>0*/
35         %if &n>0 %then %do;
36          data want;
37          set have;
38          run;
39         %else %do;
ERROR: There is no matching %IF statement for the %ELSE.
ERROR: A dummy macro will be compiled.
40         /*If n=0 then do the below*/
41          data want;
42          if 0 then set have;
43          call missing(of _all);
44          output;
45          stop;
46          run;
47          %end;
48         %mend check_and_do;
49         
50         %check_and_do
WARNING: Apparent invocation of macro CHECK_AND_DO not resolved.
novinosrin
Tourmaline | Level 20

Hi @Babloo   I missed to add a %end . Sorry about that. Here is a tested sample

 

data sample;
set sashelp.class(obs=0);
run;


%macro check_and_do(dsn=SAMPLE);
/*Get the number of obs in macro var N*/
proc sql noprint;
select nobs into :n trimmed
from dictionary.tables 
where libname='WORK' and memname="&dsn";
quit;

%put &=n;/*N is the number of obs in HAVE*/
/*Check if N>0*/
%if &n>0 %then %do;
 data want;
 set &dsn;
 run;
 %end;
%else %do;
/*If n=0 then do the below*/
 data want;
 if 0 then set &dsn;
 call missing(of _all_);
 output;
 stop;
 run;
 %end;
%mend check_and_do;

%check_and_do

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 8 replies
  • 1329 views
  • 2 likes
  • 3 in conversation