BookmarkSubscribeRSS Feed
CarolBarahona
Fluorite | Level 6

Hi all!


I'm confused between to procedure that I have to do. I think first I have to transpose my dataset and then calculate the frequency by proc tabulate, but I have not idea how to do it.


My dataset is something like this, but a little bit more complicated:

(every row is independient)

 

 

Year1Year2Year3Gender
1A1A1AA
3A  A
 2A3AB
2A3A3AB
1A 1AA

 

And I need create something like this:

 

GenderYearVarFrequency 
AYear11A2
AYear12A0
AYear13A1
AYear21A1
AYear22A0
AYear23A0
AYear31A2
AYear32A0
AYear33A0
BYear11A0
BYear12A1
BYear13A0
BYear21A0
BYear22A1
BYear23A1
BYear31A0
BYear32A0
BYear33A2

 

It's meant tabulate the frequency for every combination of my variables (gender, year and "var").

 

Thank's! 🙂

3 REPLIES 3
art297
Opal | Level 21

Here is a way to do it using a combination of proc format, proc transpose, two datasteps and proc means:

data have;
  input (Year1 Year2 Year3 Gender) ($);
  cards;
1A 1A 1A A
3A . . A
. 2A 3A B
2A 3A 3A B
1A . 1A A
;

proc format;
  value $gender
  'A'='A'
  'B'='B'
  ;
  value $year
  'Year1'='Year1'
  'Year2'='Year2'
  'Year3'='Year3'
  ;
  value $var
  '1A'='1A'
  '2A'='2A'
  '3A'='3A'
  ;
run;

data need;
  set have;
  recnum=_n_;
run;

proc transpose data=need out=need
   (drop=recnum rename=(_name_=year col1=Var));
  var Year1 Year2 Year3;
  by recnum Gender;
run;

data need;
  set need;
  count=1;
run;

PROC MEANS DATA=need completetypes nway NOPRINT;
  FORMAT gender $gender. year $year. var $var.;
  CLASS gender year var/preloadfmt;
  VAR_count;
  OUTPUT OUT=want (drop=_ :)  N=count;
RUN;

Art, CEO, AnalystFinder.com

 

Ksharp
Super User
data have;
  input (Year1 Year2 Year3 Gender) ($);
  cards;
1A 1A 1A A
3A . . A
. 2A 3A B
2A 3A 3A B
1A . 1A A
;
data temp;
 set have;
 array x{*} $ _character_;
 do i=1 to dim(x);
  name=vname(x{i});
  value=x{i};
  if not missing(value) then output;
 end;
 drop i;
run;
proc freq data=temp noprint;
table name*value/out=want sparse list nocum nopercent;
run;
Ksharp
Super User
There is a problem in my above code.   use      this :  



data have;
  input (Year1 Year2 Year3 Gender) ($);
  cards;
1A 1A 1A A
3A . . A
. 2A 3A B
2A 3A 3A B
1A . 1A A
;
data temp;
 set have;
 array x{*} $ year:;
 do i=1 to dim(x);
  name=vname(x{i});
  value=x{i};
  if not missing(value) then output;
 end;
 drop i;
run;
proc freq data=temp noprint;
table gender*name*value/out=want sparse list nocum nopercent;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1427 views
  • 0 likes
  • 3 in conversation