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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 544 views
  • 1 like
  • 2 in conversation