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

I want to capture only the error records based on some condition in one seperate variable. So I tried the code as below, but it is not producing the desired Output.

 

data have;
infile datalines;
input id $ sex $ age $;
datalines;
. M 21
2 F 23
23 M 21
;
run;

data want (drop=separator);
length Faulty_Record $ 200;
retain id sex age Faulty_Record;
set have;
if id = " " then do;
    separator=' ';
    Faulty_Record = catx(separator,of _all_);
    err_message = "ID Variable cannot be missing";
end;
run;

Output of WANT dataset is,

 

 

Faulty_Recordidsexageerr_message
M 21 M21ID Variable cannot be missing
M 212F23 
M 2123M21 

 

Desired Output WANT dataset should be,

 

Faulty_Recordidsexageerr_message
M 21 M21ID Variable cannot be missing
 2F23 
 23MH 

 

On a seperate note, I'm not sure why retain Statement is not arranging the variables in the order which I gave in retain Statement.

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

The RETAIN statement is used to retain variables, so that they are not set to missing at each iteration of the datastep. That is why your Faulty_Record is set for all records. But it is true that you can also use the RETAIN statement to change the order of variables.

 

You can get the result you want like this:

data want;
set have;
length Faulty_Record $200;
if id = " " then do;
Faulty_Record = catx(' ',of _all_);
err_message = "ID Variable cannot be missing";
end;
run;

In other words: Declare the faulty record after reading the input, so that the input data variables are created first. And no need for a special variable for the CATX separator, just use a constant.

 

 

View solution in original post

1 REPLY 1
s_lassen
Meteorite | Level 14

The RETAIN statement is used to retain variables, so that they are not set to missing at each iteration of the datastep. That is why your Faulty_Record is set for all records. But it is true that you can also use the RETAIN statement to change the order of variables.

 

You can get the result you want like this:

data want;
set have;
length Faulty_Record $200;
if id = " " then do;
Faulty_Record = catx(' ',of _all_);
err_message = "ID Variable cannot be missing";
end;
run;

In other words: Declare the faulty record after reading the input, so that the input data variables are created first. And no need for a special variable for the CATX separator, just use a constant.

 

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 1 reply
  • 409 views
  • 1 like
  • 2 in conversation