Hi everyone, hope you're ok!
I have a question for a work I am just doing:
I want to create 2 data set (appel and appel_rejet)
appel is my normal dataset if none of (Id_Client Date_appel Id_Direction Id_Distance ) is missing and appel_rejet is a data of reject value if the line contains null value for :
Id_Client Date_appel Id_Direction Id_Distance and want to add a column in appel_rejet to show the raison of reject value.
data attached ...
Can anyone help me ?
Not fully tested, remove datalines and change the infile-statement according to your environment:
data appel(drop=reason) appel_rejet;
attrib
Id_Client length=$ 10
Date_appel length=8 format=yymmddd10. informat=yymmdd10.
Heure_appel length=8 format=time5. informat=time.
No_appelant length=$ 20
No_appele length=$ 20
Id_Direction length= 8
Id_Produit length= 8
Id_Distance length= 8
Duree length= 8
Reason length= $ 130
_var length= $ 32
_value length= $20
;
drop _var _value;
infile datalines missover delimiter='|';
input Id_Client Date_appel Heure_appel No_appelant No_appele Id_Direction Id_Produit Id_Distance Duree;
/* Id_Client Date_appel Id_Direction Id_Distance */
if not missing(Id_Client) and nmiss(Date_appel, Id_Direction, Id_Distance) = 0 then do;
output appel;
end;
else do;
do _Var = 'Id_Client', 'Date_appel', 'Id_Direction', 'Id_Distance';
_value = compress(vvaluex(_Var), '.');
if missing(_value) then do;
Reason = catx(', ', Reason, _Var);
end;
end;
output appel_rejet;
end;
datalines;
224688|2003-01-03|15:39|0548967125|0666778899|1|1|3|78
224688|2003-01-08|15:52|0612312345|0666778899|1|2|1|1
224688|2003-01-09|23:00|0666778899|0548967125|2|1|4|150
224688|2003-01-11|16:28|0666778899|0614759648|2|2|2|1
224688|2003-01-15|02:30|0666778899|0548967125|2|3|3|1
224688|2003-01-18|04:01|0666778899|0612312345|2|1|1|135
355557|2003-01-24|16:02|0615151515|0655446655|1|1|1|256
355557|2003-01-26|10:50|0664972835|0655446655|1|1|2|948
355557|2003-01-18|01:03|0664972844|0655446655|1|2|2|1
355557|2003-01-30| |0655446655|+4946497|2|1|5|145
958462|2003-01-24|05:23|0699887766|0247965814|2|3|4|1
958462|2003-01-27|23:18|0612312345|0699887766|1|1|1|248
121212|2003-01-01|11:04|06046728913|0612312345|1|1|2|648
121212|2003-01-14|15:39|0646728914|0612312345|1|2|2|1
121212|2003-01-23|15:43|0612312345|0627273838|2|1|1|254
121212|2003-01-28|23:23|0612312345|+415548967|2|3|5|1
773377|2003-01-02|18:02|0627273838|0611223344|1|1|1|694
876543|2003-01-13|04:13|0635483548|0622446688|1|1|2|425
876543|2003-01-16|07:57|0627273838|0622446688|1|1|1|348
876543|2003-01-16|21:29|0622446688|0635483548|2|1|2|648
876543|2003-01-22|16:26|0622446688|+33145784875|2|3|5|1
876543|2003-01-05|10:42|0622446688|0627273838|2|1|1|248
876543|2003-01-31|18:31|0622446688|0635483548|2|1|2|1045
876543|2003-01-24|04:27|0622446688|0627273838|3|2| |1
876543|2003-01-16|23:30|0627273838|0622446688|1| |1|1
876543|2003-01-18|11:53|0635483548|0622446688|1|1|2|318
654321|2003-01-07|16:04|+49487569|0611223344|1|1|5|648
654321|2003-01-04|17:40|0699887766|0611223344|1|2|1|1
654321|2003-01-19|01:48|0699887766|0611223344|1|1|1|964
654321|2003-01-21|20:26|1645889545|0611223344|1|1|1|478
654321|2003-01-06|08:11|0611223344|0654789658|2|1|2|1845
654321|2003-01-13|10:42|0611223344|0664849452|2|2|2|1
654321|2003-01-16|01:43|0611223344|0345784875|2|3|4|1
654321|2003-01-24|18:43|0611223344|0699887766|2|1|1|314
654321|2003-01-26|01:12|0611223344|0645889545|2|1|1|695
654321|2003-01-17|11:06|0611223344|0654789658|2|1|2|148
654321|2003-01-22|06:19|0611223344|0345784875|2|3|4|1
654321|2003-01-15|23:43|0622446688|0611223344|1|1|1|318
654321|2003-01-20|12:43|0622446688|0611223344|1|1|1|648
444333|2003-01-18|18:13|0684758475|0615151515|1| |2|145
444333|2003-01-29|18:45|0684758476|0615151515|1|2|2|1
444333|2003-01-12|04:39|0684758477|0615151515|1|1|2|364
444333|2003-01-15|22:55|+457894875|0615151515|1|1|5|1520
444333|2003-01-18|11:06|0214578946|0615151515|1|1|3|648
444333|2003-01-24|11:52|0664648585|0615151515|1|2|2|1
444333| | |0615151515|0214578946| | |3|315
666999| |18:27|0664648585|0669696969|1|1|2|348
234555|2003-01-22|18:03|0612345678|0214579468|2|3|4|1
234555|2003-01-18|04:57|0612345678|0622446688|2|1|1|648
234555|2003-01-26|08:59|0612345678|1622446688|2|1|1|695
234555|2003-01-04|22:32|0612345678|0697154863|2|1| |842
234555| |08:36|0612345678|0645798524|2|1| |3641
;
run;
You can do this with multiple datasets on the data line and conditional output statements. Something like this?
data goodshoes badshoes; set sashelp.shoes; badstoreflag = 0; if sales < 5000 then badstoreflag = 1; else if sales < 10000 then badstoreflag = 2; if badstoreflag = 0 then output goodshoes; else output badshoes; run;
Not fully tested, remove datalines and change the infile-statement according to your environment:
data appel(drop=reason) appel_rejet;
attrib
Id_Client length=$ 10
Date_appel length=8 format=yymmddd10. informat=yymmdd10.
Heure_appel length=8 format=time5. informat=time.
No_appelant length=$ 20
No_appele length=$ 20
Id_Direction length= 8
Id_Produit length= 8
Id_Distance length= 8
Duree length= 8
Reason length= $ 130
_var length= $ 32
_value length= $20
;
drop _var _value;
infile datalines missover delimiter='|';
input Id_Client Date_appel Heure_appel No_appelant No_appele Id_Direction Id_Produit Id_Distance Duree;
/* Id_Client Date_appel Id_Direction Id_Distance */
if not missing(Id_Client) and nmiss(Date_appel, Id_Direction, Id_Distance) = 0 then do;
output appel;
end;
else do;
do _Var = 'Id_Client', 'Date_appel', 'Id_Direction', 'Id_Distance';
_value = compress(vvaluex(_Var), '.');
if missing(_value) then do;
Reason = catx(', ', Reason, _Var);
end;
end;
output appel_rejet;
end;
datalines;
224688|2003-01-03|15:39|0548967125|0666778899|1|1|3|78
224688|2003-01-08|15:52|0612312345|0666778899|1|2|1|1
224688|2003-01-09|23:00|0666778899|0548967125|2|1|4|150
224688|2003-01-11|16:28|0666778899|0614759648|2|2|2|1
224688|2003-01-15|02:30|0666778899|0548967125|2|3|3|1
224688|2003-01-18|04:01|0666778899|0612312345|2|1|1|135
355557|2003-01-24|16:02|0615151515|0655446655|1|1|1|256
355557|2003-01-26|10:50|0664972835|0655446655|1|1|2|948
355557|2003-01-18|01:03|0664972844|0655446655|1|2|2|1
355557|2003-01-30| |0655446655|+4946497|2|1|5|145
958462|2003-01-24|05:23|0699887766|0247965814|2|3|4|1
958462|2003-01-27|23:18|0612312345|0699887766|1|1|1|248
121212|2003-01-01|11:04|06046728913|0612312345|1|1|2|648
121212|2003-01-14|15:39|0646728914|0612312345|1|2|2|1
121212|2003-01-23|15:43|0612312345|0627273838|2|1|1|254
121212|2003-01-28|23:23|0612312345|+415548967|2|3|5|1
773377|2003-01-02|18:02|0627273838|0611223344|1|1|1|694
876543|2003-01-13|04:13|0635483548|0622446688|1|1|2|425
876543|2003-01-16|07:57|0627273838|0622446688|1|1|1|348
876543|2003-01-16|21:29|0622446688|0635483548|2|1|2|648
876543|2003-01-22|16:26|0622446688|+33145784875|2|3|5|1
876543|2003-01-05|10:42|0622446688|0627273838|2|1|1|248
876543|2003-01-31|18:31|0622446688|0635483548|2|1|2|1045
876543|2003-01-24|04:27|0622446688|0627273838|3|2| |1
876543|2003-01-16|23:30|0627273838|0622446688|1| |1|1
876543|2003-01-18|11:53|0635483548|0622446688|1|1|2|318
654321|2003-01-07|16:04|+49487569|0611223344|1|1|5|648
654321|2003-01-04|17:40|0699887766|0611223344|1|2|1|1
654321|2003-01-19|01:48|0699887766|0611223344|1|1|1|964
654321|2003-01-21|20:26|1645889545|0611223344|1|1|1|478
654321|2003-01-06|08:11|0611223344|0654789658|2|1|2|1845
654321|2003-01-13|10:42|0611223344|0664849452|2|2|2|1
654321|2003-01-16|01:43|0611223344|0345784875|2|3|4|1
654321|2003-01-24|18:43|0611223344|0699887766|2|1|1|314
654321|2003-01-26|01:12|0611223344|0645889545|2|1|1|695
654321|2003-01-17|11:06|0611223344|0654789658|2|1|2|148
654321|2003-01-22|06:19|0611223344|0345784875|2|3|4|1
654321|2003-01-15|23:43|0622446688|0611223344|1|1|1|318
654321|2003-01-20|12:43|0622446688|0611223344|1|1|1|648
444333|2003-01-18|18:13|0684758475|0615151515|1| |2|145
444333|2003-01-29|18:45|0684758476|0615151515|1|2|2|1
444333|2003-01-12|04:39|0684758477|0615151515|1|1|2|364
444333|2003-01-15|22:55|+457894875|0615151515|1|1|5|1520
444333|2003-01-18|11:06|0214578946|0615151515|1|1|3|648
444333|2003-01-24|11:52|0664648585|0615151515|1|2|2|1
444333| | |0615151515|0214578946| | |3|315
666999| |18:27|0664648585|0669696969|1|1|2|348
234555|2003-01-22|18:03|0612345678|0214579468|2|3|4|1
234555|2003-01-18|04:57|0612345678|0622446688|2|1|1|648
234555|2003-01-26|08:59|0612345678|1622446688|2|1|1|695
234555|2003-01-04|22:32|0612345678|0697154863|2|1| |842
234555| |08:36|0612345678|0645798524|2|1| |3641
;
run;
@andreas_lds , thanks so much , it works perfectly 😍🕺
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.