I tried the code as follows and ended up with error. If there is any observation in the dataset then I want to get that value in 'vars' dataset and if no Observation I want to get missing value.
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.
ELSE must be the next command after the IF.
Or ... If you use the structure IF ... THEN ... DO ... END, then the ELSE must be the next command after the END
You have a statement between the IF and the ELSE.
Note that you do NOT need the ELSE. Your IF condition is always going to be false so whatever statement you wanted to put after the ELSE can just be run unconditionally since you want it to always run.
data vars;
if 0 then set dis_entity_id;
call missing(of _all_);
set dis_entity_id;
It looks like you are just trying create a dataset with one observation that has all missing values. (why???)
That can be done then use one of these. The shorter one will get a NOTE in the log about stopping because of looping.
data vars;
if 0 then set dis_entity_id;
run;
data vars;
if 0 then set dis_entity_id;
output;
stop;
run;
If you want to create a dataset with zero observations use one of these:
data vars;
set dis_entity_id;
stop;
run;
data vars;
set dis_entity_id(obs=0);
run;
data vars;
if 0 then set dis_entity_id; else set dis_entiti_id;
call missing(of _all_);
output;
stop;
run;
this should work fine . if and else statement shall come together .
@Babloo wrote:
May I know for which condition the 'call missing' will execute? I want it
to execute only if obs is 0 in the source dataset.
Doesn't really make any sense. If there are no values then the values are already missing.
What output do you want when there are 0 observations in the source? Do you want 0 observations, 1 observation, something else?
What output do you want when there is only 1 observation in the source?
What output do you want when there are more than 1 observation in the source?
@Babloo wrote:
I want to display missing value for a variable only if source dataset has 0
observation.
If the source dataset has 1 or more than 1 observations then I want those
observation(s) in the output.
You are still not describing what you want completely. Examples really help.
The only reason to do ANYTHING is if you want to output 1 observation when there are NO observations in the source.
You could use the NOBS= option on SET statement to check how many observations, but that will not work for views and other types of datasets where the metadata of the number of observations is not available. It is probably better to use the END= option as that will work for all data sources.
So you need to conditionally add an OUTPUT statement to get the extra observation created. But once you have an OUTPUT statement you need to add one at the end of the step to replace the implicit OUTPUT that is normally added to a data step. Also you need test BEFORE the SET statement because once the SET statement reads past the end of the input data the step stops. So any statements after the SET will never execute if the input dataset is empty.
data want ;
if _n_=1 and eof then output;
set have end=eof;
output;
run;
Note you can simplify (obfuscate?) by changing the IF condition and eliminate the need for the extra OUTPUT statement.
data want;
if (_n_>1) or eof then output;
set have end=eof;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: