BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rohit12
Obsidian | Level 7

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 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

 

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20

What are you trying to achieve?

Data never sleeps
Rohit12
Obsidian | Level 7

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

Rohit12
Obsidian | Level 7
data new;
input name $;
datalines;
RET_AKT
RET_HIS
RET_AKT
RET_AKT
AMU_NEW
AMU_OLD
;
run;

%let x=RET_AKT;

data new1;
if name="&X" then do;
set new;
where name="&X";
output;
end;
else if name='' then do;
set new;
output;
end;
run;

why it is showing 4 rows in the output
mnjtrana
Pyrite | Level 9

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


Cheers from India!

Manjeet
Daniel-Santos
Obsidian | Level 7

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

Astounding
PROC Star

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.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 7409 views
  • 1 like
  • 5 in conversation