Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

Re: Do's and loops

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

Re: Do's and loops

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


Accepted Solutions
Solution
‎03-21-2014 01:16 AM
Trusted Advisor
Posts: 1,129

Re: Do's and loops

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


All Replies
Super User
Super User
Posts: 7,401

Re: Do's and loops

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;

Trusted Advisor
Posts: 1,129

Re: Do's and loops

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
Contributor
Posts: 29

Re: Do's and loops

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

Valued Guide
Posts: 765

Re: Do's and loops

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.

Contributor
Posts: 57

Re: Do's and loops

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

Solution
‎03-21-2014 01:16 AM
Trusted Advisor
Posts: 1,129

Re: Do's and loops

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
Contributor
Posts: 57

Re: Do's and loops

Dear Jagadish Sir,

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

Super User
Posts: 6,938

Re: Do's and loops

Why do you have the 3 if/else's when you do the same anyway?

It's always hh_idc=put(hh_id,z3.);

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,129

Re: Do's and loops

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
Contributor
Posts: 74

Re: Do's and loops

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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