- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
" "
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
if last_record then do;
enity_nm="";
output;
end;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I tried and it didn't work as I got 0 observation
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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