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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag

View solution in original post

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
Chrishi
Calcite | Level 5

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.)); 

MikeZdeb
Rhodochrosite | Level 12

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.

anilgvdbm
Quartz | Level 8

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

Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
anilgvdbm
Quartz | Level 8

Dear Jagadish Sir,

Thank you very much for your Idea. it's perfect what i want.

Jagadishkatam
Amethyst | Level 16

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

Thanks,
Jag
TarunKumar
Pyrite | Level 9

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to choose a machine learning algorithm

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.

Discussion stats
  • 10 replies
  • 2070 views
  • 5 likes
  • 7 in conversation