Help using Base SAS procedures

create dataset want with a particular structure from

Accepted Solution Solved
Reply
Super Contributor
Posts: 301
Accepted Solution

create dataset want with a particular structure from

Hi everyone, anyone can help me with this?;

I I have got the dataset have, and I want to convert it in a dataset want , please find below *;

data have;

  input subjid siteid param $ response $;

  datalines;

  1 100 u a

  1 100 v a

  1 100 w a

  1 100 u b

  1 100 v b

  1 100 w b

  2 200 u a

  2 200 v a

  2 200 w a

  3 300 u a

  3 300 v a

  3 300 w a

  3 300 u b

  3 300 v b

  3 300 w b

  ;

  run;

I would like this kind of dataset want, with this structure *:

                       response a           response b

site 100                                         u  v  w

                           u

                           v

                          w

site 200              u

                          v

                         w

site 300             u

                         v

                        w

Thanks in advance,

V.


Accepted Solutions
Solution
‎03-01-2013 02:08 PM
Respected Advisor
Posts: 3,156

Re: create dataset want with a particular structure from

data want;

  do until (last.siteid);

  set have;

by siteid;

length site response_a response_b $20.;

site=catx(' ','site',siteid);

if response='b' then response_b=catx(' ',response_b,param); else response_b=response_b;

  end;

  do _n_=1 by 1 until (last.siteid);

  set have;

by siteid;

  if _n_>1 then call missing (site,response_b);

if response='a' then response_a=param; else response_a='';

if not missing(response_a) then output;

end;

keep site response_:;

run;

Haikuo

View solution in original post


All Replies
Super User
Posts: 19,772

Re: create dataset want with a particular structure from

Can you provide your want structure as a data step as well. It's hard to visualize how you want it. Also, is this to be displayed or in a dataset? What should the blanks be?

Respected Advisor
Posts: 3,156

Re: create dataset want with a particular structure from

It an't pretty, but seems does what you want.

data have;

  input subjid siteid param $ response $;

  datalines;

  1 100 u a

  1 100 v a

  1 100 w a

  1 100 u b

  1 100 v b

  1 100 w b

  2 200 u a

  2 200 v a

  2 200 w a

  3 300 u a

  3 300 v a

  3 300 w a

  3 300 u b

  3 300 v b

  3 300 w b

  ;

  run;

  data want;

  do until (last.siteid);

  set have;

by siteid;

length site response_a response_b $20.;

site=catx(' ','site',siteid);

response_b=ifc(response='b',catx(' ',response_b,param), response_b);

  end;

  do _n_=1 by 1 until (last.siteid);

  set have;

by siteid;

  if _n_>1 then call missing (site,response_b);

response_a=ifc(response='a',param,'');

if not missing(response_a) then output;

end;

keep site response_:;

run;

Haikuo

Super Contributor
Posts: 301

Re: create dataset want with a particular structure from

Hi Haikuo,

please , could you supply the function ifc for another line of code to see if you generate the right dataset?,

because IFC  does not work in my 9.0 SAS version.

Thanks,

Jose.

Solution
‎03-01-2013 02:08 PM
Respected Advisor
Posts: 3,156

Re: create dataset want with a particular structure from

data want;

  do until (last.siteid);

  set have;

by siteid;

length site response_a response_b $20.;

site=catx(' ','site',siteid);

if response='b' then response_b=catx(' ',response_b,param); else response_b=response_b;

  end;

  do _n_=1 by 1 until (last.siteid);

  set have;

by siteid;

  if _n_>1 then call missing (site,response_b);

if response='a' then response_a=param; else response_a='';

if not missing(response_a) then output;

end;

keep site response_:;

run;

Haikuo

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 163 views
  • 0 likes
  • 3 in conversation