Need required o/p as mentioned below

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

Need required o/p as mentioned below

Hi,

I have input data as below

Statesdistricts state codedistricts code
haryanasirsa34352
haryanagurgoan34350
himachal pradeshLahul & spiti46474
himachal pradeshkinnaur46476

And i need output data as per below format.

sr noDistrictscodes
haryana34
1sirsa352
2gurgoan350
himachal pradesh46
1Lahul & spiti474
2kinnaur476

Can anyone please suggest how to get it.

Thanks in advance for your support.


Accepted Solutions
Solution
‎11-22-2013 07:54 AM
Regular Contributor
Posts: 213

Re: Need required o/p as mentioned below

OK, again assuming the input data set called input.

proc sort data=input; by state_code districts_code; run;

data outDs(keep=sr_no District codes);

    LENGTH sr_no 4 District $20 codes 8;

    LABEL    sr_no ='sr no'

            District = 'Districts'

            codes = 'Codes';

    SET input;

    BY state_code districts_code;

    if first.state_code then

    do;

        cnt      = 0;

        District = states;

        codes    = state_code;

        output;

    end;

    cnt+1;

    sr_no    = cnt;

    District = districts;

    codes    = districts_code;

    output;

run;

options missing='';

PROC PRINT data=outDs noobs label;

RUN;

View solution in original post


All Replies
Respected Advisor
Posts: 3,899

Re: Need required o/p as mentioned below

What have you tried so far. Can you share some code and then let us know where you've got stuck?

Contributor
Posts: 61

Re: Need required o/p as mentioned below

Hi Patrick,

I haven't got any approch to get start. Can you please suggest something.

Regular Contributor
Posts: 213

Re: Need required o/p as mentioned below

Here is a sample code

assuming the input data set called input.

proc sort data=input; by state_code districts_code; run;

filename test 'c:\temp\testoutput.txt';

data _null_;

    file test;

    set input;

    by state_code districts_code;

    if (_n_=1) then

    do;

        put 'sr no' '09'x 'Districts' '09'x    'codes';

    end;

       

    if first.state_code then

    do;

        cnt=0;

        put '09'x states '09'x state_code;

    end;

    cnt+1;

    put cnt '09'x districts '09'x districts_code;

run;

Contributor
Posts: 61

Re: Need required o/p as mentioned below

Hi Ahmed,

Thanks for sample code..Its working but i don't want to write to text file or use put statement. I need to store it in dataset and from that i need to generate report. If possible can you please suggest.

Thanks in advance.

Solution
‎11-22-2013 07:54 AM
Regular Contributor
Posts: 213

Re: Need required o/p as mentioned below

OK, again assuming the input data set called input.

proc sort data=input; by state_code districts_code; run;

data outDs(keep=sr_no District codes);

    LENGTH sr_no 4 District $20 codes 8;

    LABEL    sr_no ='sr no'

            District = 'Districts'

            codes = 'Codes';

    SET input;

    BY state_code districts_code;

    if first.state_code then

    do;

        cnt      = 0;

        District = states;

        codes    = state_code;

        output;

    end;

    cnt+1;

    sr_no    = cnt;

    District = districts;

    codes    = districts_code;

    output;

run;

options missing='';

PROC PRINT data=outDs noobs label;

RUN;

Contributor
Posts: 61

Re: Need required o/p as mentioned below

Hi Ahmed, Its working thanks a lot for your help.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 275 views
  • 5 likes
  • 3 in conversation