data new;
input name $;
datalines;
RET_AKT
RET_HIS
RET_AKT
RET_AKT
AMU_NEW
AMU_OLD
;
run;
data new1;
if name='RET_AKT' then do;
set new;
where name='RET_AKT';
end;
else if name='' then do;
set new;
end;
run;
WARNING: Multiple lengths were specified for the variable name by input data set(s). This may
cause truncation of data.
NOTE: There were 3 observations read from the data set WORK.NEW.
WHERE name='RET_AKT';
NOTE: There were 1 observations read from the data set WORK.NEW.
NOTE: The data set WORK.NEW1 has 4 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
Question . why it is showing 4 obeservation in output dataset new1 not 3 observation. I want 3 observation in the dataset new1.why warning is coming
To answer your original question, why do you get 4 observations ...
As the DATA step begins, NAME is missing. Therefore, the first SET statement is skipped, and the second one executes. It reads the first observation from NEW (changing NAME to "RET_AKT") which becomes the first observation in NEW1.
From that point forward, NAME is always "RET_AKT".
Therefore, for the rest of the DATA step, the first SET statement executes. Each SET statement is independent of the other, so the first SET statement begins at the start of NEW and (one at a time) reads in three observations. Those become the 2nd through 4th observations in NEW1.
It would be easier for you to see this if you were to add another variable. For example, in the creation of NEW, add:
obsno = _n_;
Then you could look at OBSNO to see where the observations in NEW1 are coming from.
What are you trying to achieve?
I will create a macro variable which will have column 'name' value or it will be blank
for ex
let x =RET_AKT;
After that I want to create a dataset which will take this macro variable value and compare this macro variable value with the column 'name' if value is equal then it will create the dataset 'NEW1' with where name=&x and if value of macro variable is blank then it will copy all the records from dataset 'new' to dataset 'new1'
for ex
if let x=RET_AKT; then it will copy 3 records from dataset new to dataset new1
and if let x=; then it should copy 6 records to dataset new 1
Hope I able to explain
Hey,
If i understood the question correctly, you want to filter out the data based on the name value of- 'RET_AKT'.
You are doing the logic incorrectly,
data new;
input name $;
datalines;
RET_AKT
RET_HIS
RET_AKT
RET_AKT
AMU_NEW
AMU_OLD
;
run;
data new1;
set new;
where name='RET_AKT';
run;
the warning is coming, because when you created the dataset new, the default length of 8 byte is assigned . However when you created the datset new1, you didn't assign the length, so it took the length from the first assignement of name variable, ie. from
if name='RET_AKT' then do; which will only have a length of 7 bytes.
WARNING: Multiple lengths were specified for the variable name by input data set(s). This may
cause truncation of data.
Hoep this helps!
Manjeet
Small correction to @mnjtrana solution.
%let X=RET_AKT;
data new1;
set new;
where NAME="&X" or "&X" = '';
run;
So, if NAME=X then dataset is filtered else X='' then all obs are kept.
Hope it helps.
Daniel Santos @ www.cgd.pt
To answer your original question, why do you get 4 observations ...
As the DATA step begins, NAME is missing. Therefore, the first SET statement is skipped, and the second one executes. It reads the first observation from NEW (changing NAME to "RET_AKT") which becomes the first observation in NEW1.
From that point forward, NAME is always "RET_AKT".
Therefore, for the rest of the DATA step, the first SET statement executes. Each SET statement is independent of the other, so the first SET statement begins at the start of NEW and (one at a time) reads in three observations. Those become the 2nd through 4th observations in NEW1.
It would be easier for you to see this if you were to add another variable. For example, in the creation of NEW, add:
obsno = _n_;
Then you could look at OBSNO to see where the observations in NEW1 are coming from.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.