Dear Friends,
here i have three variables called village_id, household_id and individual_id so from these 3 variables i need to create one unique id so here i have a small problem like
when i use cat function
vill_id hh_id indv_id Answer_required
1 23 2 12302
1 54 1
2 45 3
2 28 9
3 87 4
3 4 6 30406
4 10 5
4 91 7
so please tel me how to use do and if and then statements.
Thanks
Anil
Please try
data have;
length hh_idc $10.;
input hh_id ;
if hh_id < 9 then hh_idc=put(hh_id,z3.);
else if 10 < hh_id < 99 then hh_idc=put(hh_id,z3.);
else hh_idc=put(hh_id,z3.);
cards;
1
6
2
18
280
;
Thanks,
Jag
Hi,
data have;
attrib vill_id hh_id indv_id format=best.;
vill_id=1; hh_id=23; indv_id=2;
output;
run;
data want;
set have;
attrib answer_required format=$20.;
answer_required=strip(put(vill_id,best.))||strip(put(hh_id,best.))||strip(put(indv_id,z2.));
run;
I am not sure of the exact output , so here is the code which i believe has produced the desired output. Please try and check the output. Let me know if it isn't the desired output.
data test;
input vill_id hh_id indv_id ;
answer=cats(put(vill_id,best.),put(hh_id,z2.),put(indv_id,z2.));
datalines;
1 23 2
1 54 1
2 45 3
2 28 9
3 87 4
3 4 6
4 10 5
4 91 7
;
run;
Thanks,
Jag
Try this code it should work fine even if using cat :-
Answer_required=cat(put(vill_id,2.),put(hh_id,z2.),put(indv_id,z2.));
Hi ... might also add a LENGTH statement for the new variable ...
LENGTH Answer_required $6;
Answer_required=cat(put(vill_id,2.),put(hh_id,z2.),put(indv_id,z2.));
Otherwise, its length would be the default of 200.
Dear All,
Actually i have household id's 1-300 so in that i need to add 2 zeros in front of 1-9 and 1 zero infront of 10-99 and i dont ned for 100 to 300.
so please help in how to do this.
Example:
hh_id i need
1 001
6 006
2 002
18 018
280 218
Regards,
Anil
Please try
data have;
length hh_idc $10.;
input hh_id ;
if hh_id < 9 then hh_idc=put(hh_id,z3.);
else if 10 < hh_id < 99 then hh_idc=put(hh_id,z3.);
else hh_idc=put(hh_id,z3.);
cards;
1
6
2
18
280
;
Thanks,
Jag
Dear Jagadish Sir,
Thank you very much for your Idea. it's perfect what i want.
Why do you have the 3 if/else's when you do the same anyway?
It's always hh_idc=put(hh_id,z3.);
I totally agree with you KurtBremser. Thanks a lot.
@Anil, please consider the below code
data have;
length hh_idc $10.;
input hh_id ;
if hh_id ne . then hh_idc=put(hh_id,z3.);
cards;
1
6
2
18
280
;
Thanks,
jag
data test;
input vill_id hh_id indv_id ;
datalines;
1 23 2
1 54 1
2 45 3
2 28 9
3 87 4
3 4 6
4 10 5
4 91 7
;
run;
data want;
set test;
if length(compress(put(indv_id,2.))) < 2 then temp_indv_id = compress('0'||indv_id);
else temp_indv_id = compress(indv_id);
if length(compress(put(hh_id,2.))) < 2 then temp_hh_id = compress('0'||hh_id);
else temp_hh_id = compress( hh_id);
Answer_required = compress( put(vill_id,1.)||temp_hh_id||temp_indv_id);
drop temp_hh_id temp_indv_id;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.