Solved
Contributor
Posts: 41

# Histograms over time with loops across the existing variables

Dear all,

I am trying to create a histogram (with kernel and mean and std. deviation measurements) for a number of variables over time. So that we have percent on y=axis and years on x-axis. Plus I need to make sure that in the title of each histogam I have the description of each variable. Does anyone know how to do it?

For example, the data should look like this:

Variable     Description                  2005      2006       2007

X1              Current Asset 1           0.07        0.01       0.90

X2              Current Asset 2           0.86       0.2          0.03

X3              Current Asset 3           0.45      0.4           0.5

Thank you

Accepted Solutions
Solution
‎07-20-2016 12:39 PM
Super User
Posts: 23,789

## Re: Histograms over time with loops across the existing variables

[ Edited ]

This should give you the idea. You can use a similar idea with proc univariate.

``````data have;
informat variable \$2. description \$25.;
input Variable \$ Description \$ Y2005 Y2006 Y2007;
cards;
X1 Current_Asset_1 0.07 0.01 0.90
X2 Current_Asset_2 0.86 0.2 0.03
X3 Current_Asset_3 0.45 0.4 0.5
;
run;

*transpose to long;
data long;
set have;
array years(2005:2007) y2005-y2007;
do Year=2005 to 2007;
Value=years(year);
output;
end;
drop y20:;
run;

*sort on description;
proc sort data=long;
by description year;
run;

*Graph using vbarparm;
proc sgplot data=long;
title1 '#byval1';
by Description;
vbarparm category=year response=value;
run;quit;``````

All Replies
Super User
Posts: 23,789

## Re: Histograms over time with loops across the existing variables

A histogram over time? A histogram usually only has one variable and shows the distribution of that variable. I'm not understanding how you want to incorporate time.

Contributor
Posts: 41

## Re: Histograms over time with loops across the existing variables

Hello, Reeza,

I guess they called it histograms but need two things; (1) the % coverage of one variable over time; (2) histograms per year of each variable (I am not sure how to do it though).

For (1) I tried to run the following program:

proc sgplot data=have;

series x = year y = X1;

xaxis values=(2005 to 2015);

run;

For (2) I tried to run the following program:

proc univariate data=have noprint;

class year;

histogram X1 / barlabel= percent kernel(c = 0.25 0.50 0.75 1.00

noprint);

run;

Also, since I have hundreds of variables, is there a way to send all those grpahs in excel directly?

Contributor
Posts: 41

## Re: Histograms over time with loops across the existing variables

proc sgplot data=sql.Compustat_Coverage;

series x = year y = X1;

xaxis values=(2005 to 2015);

run;

If I need to run over other variables, what would be the best way. &list?

Super User
Posts: 23,789

## Re: Histograms over time with loops across the existing variables

That's not a histogram that's a scatter plot.

I would transpose my data to a long format and use BY processing. BY processing also allows for dynamic titles.

I think your going to need to do that anyways for it to work. Wouldn't the year need to be a column rather than a column for every year, which is what you have now?

Contributor
Posts: 41

## Re: Histograms over time with loops across the existing variables

I am trying but if I have a long format I cannot see the distinction of selected variable.

Super User
Posts: 23,789

## Re: Histograms over time with loops across the existing variables

Each variable would be its own chart? Isn't that what you want?

Try it, post the code if your having issues.

Contributor
Posts: 41

## Re: Histograms over time with loops across the existing variables

I am trying this now:

data want;

length variable \$32;

set have;

array var_list(*) &list;

do i = 1 to dim(var_list);

variable = vname(var_list{i});

value = var_list{i};

output;

end;

keep year variable value;

run;

proc univariate data=want noprint;

class year variable;

histogram value / barlabel= percent kernel;

run;

It is not working.

Solution
‎07-20-2016 12:39 PM
Super User
Posts: 23,789

## Re: Histograms over time with loops across the existing variables

[ Edited ]

This should give you the idea. You can use a similar idea with proc univariate.

``````data have;
informat variable \$2. description \$25.;
input Variable \$ Description \$ Y2005 Y2006 Y2007;
cards;
X1 Current_Asset_1 0.07 0.01 0.90
X2 Current_Asset_2 0.86 0.2 0.03
X3 Current_Asset_3 0.45 0.4 0.5
;
run;

*transpose to long;
data long;
set have;
array years(2005:2007) y2005-y2007;
do Year=2005 to 2007;
Value=years(year);
output;
end;
drop y20:;
run;

*sort on description;
proc sort data=long;
by description year;
run;

*Graph using vbarparm;
proc sgplot data=long;
title1 '#byval1';
by Description;
vbarparm category=year response=value;
run;quit;``````

Contributor
Posts: 41

## Re: Histograms over time with loops across the existing variables

Dear Reeza,

Thank you very much! This is exactly what I was trying to get. Thank you so much for your prompt help!

Yelena

Contributor
Posts: 41

## Re: Histograms over time with loops across the existing variables

I have transformed the data as otherwise it does not work. Not I have a column "year" and "X1" - "X3"

☑ This topic is solved.