SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
newsas007
Quartz | Level 8

Hi all,

I am trying to get ideas for generating a trend graph by year and a few other variables (4 exactly) by a categorical variable (3 categories).

the year value represents crime per thousand. The building, traffic, space, and quality variables are all survey values (higher score is better)

Any cool ideas are appreciated. Thanks

data have;
	infile datalines dlm=',';
	input Area :$5. Year_2020 Year_2021	Year_2022 Building	Traffic	Space Quality;
datalines;
A,64,114,105,12.8,15,3.6,16.6
B,7,12,14,13.9,14.7,14.8,17.5
C,1,6,4,16.9,18.5,17,19.6
;
 
8 REPLIES 8
ballardw
Super User

With SAS any sort of trend data will go much better with one observation per date (daily, monthly, yearly what ever).

 

An approach to a basic series plot based on the time value. The data step creates one observation for the apparent category variable and time point. You didn't mention what value any of those "year" variables represented. A variable name and label to make more sense in the graph display would be a very good.

Without any description of what any of Building Traffic Space Quality mean and how they would relate to a "trend" I ignore them. Some sort of meaning would be needed.

data toplot;
   set have;
   array y(2020:2022) year_2020 Year_2021 Year_2022 ;
   do year = 2020 to 2022;
      Yearvalue = y[year];
      output;
   end;
   keep area year yearvalue;
run;


proc sgplot data=toplot;
  series x=year y=yearvalue/ group=area;
run;

If I knew that the year value represented something such as "price in $1,000s" a label for such could appear on the year helping someone understand what is displayed.

newsas007
Quartz | Level 8

Thanks for this, the year value represents crime per thousand. The building, traffic, space, and quality variables are all survey values (higher score is better)

 

Ksharp
Super User

It looks like you need Mixed Model to take into account of other independent variables (Building Traffic Space Quality) for trend analysis .

Thus, better post it at Statistical Forum:

https://communities.sas.com/t5/Statistical-Procedures/bd-p/statistical_procedures

 

data have;
	infile datalines dlm=',';
	input Area :$5. Year_2020 Year_2021	Year_2022 Building	Traffic	Space Quality;
datalines;
A,64,114,105,12.8,15,3.6,16.6
B,7,12,14,13.9,14.7,14.8,17.5
C,1,6,4,16.9,18.5,17,19.6
;
data want;
 set have;
 array x{*} Year_2020 Year_2021	Year_2022;
 do i=1 to dim(x);
   y=x{i};time=input(scan(vname(x{i}),-1,'_'),best.);output;
 end;
 drop i Year_:;
run;
proc glimmix data=want;
class area time;
model y=Building	Traffic	Space Quality time/solution dist=poisson;
random time/residual subject=area  type=ar(1);
store out=MixedModel;
run;
proc plm restore=MixedModel;
effectplot interaction (x=time )/clm connect;;
run;

Ksharp_0-1723082059077.png

 

newsas007
Quartz | Level 8

Is there a 3D plot that i can perhaps use with say 2 continuous variables (Year_2022, quality) and categorical variable Area? This would be helpful

Ksharp
Super User
proc template;
define statgraph layoutoverlay3d;
begingraph;
entrytitle "Density Plot of Height and Weight";
layout overlay3d / tilt=10 rotate=54
walldisplay=none cube=false;
surfaceplotparm x=height y=weight z=density /
surfacecolorgradient=density;
endlayout;
endgraph;
end;
run;
proc sgrender data=sashelp.gridded template=layoutoverlay3d;
run;
newsas007
Quartz | Level 8
i used this code:
proc template;
define statgraph layoutoverlay3d;
begingraph;
entrytitle "Density Plot of crime and quality score";
layout overlay3d / tilt=10 rotate=54
walldisplay=none cube=false;
surfaceplotparm x=Year_2020 y=quality z=Area /
surfacecolorgradient=Area;
endlayout;
endgraph;
end;
run;
proc sgrender data=have template=layoutoverlay3d;
run;
Ksharp
Super User

Try this one. 

And you need more data to plot 3D graph.

data have;
	infile datalines dlm=',';
	input Area :$5. Year_2020 Year_2021	Year_2022 Building	Traffic	Space Quality;
datalines;
A,64,114,105,12.8,15,3.6,16.6
B,7,12,14,13.9,14.7,14.8,17.5
C,1,6,4,16.9,18.5,17,19.6
;

proc format;
invalue ifmt
'A'=1
'B'=2
'C'=3;
value fmt
1='A'
2='B'
3='C';
run;
data have;
 set have;
 _area=input(area,ifmt.);
 format _area fmt.;
run;
proc kde data=have;
bivar _Area Quality/ plots=all;
freq Year_2022;
run;

Ksharp_0-1723174067806.png

 

 

Ksharp
Super User
data have;
	infile datalines dlm=',';
	input Area :$5. Year_2020 Year_2021	Year_2022 Building	Traffic	Space Quality;
datalines;
A,64,114,105,12.8,15,3.6,16.6
B,7,12,14,13.9,14.7,14.8,17.5
C,1,6,4,16.9,18.5,17,19.6
;

proc format;
invalue ifmt
'A'=1
'B'=2
'C'=3;
value fmt
1='A'
2='B'
3='C';
run;
data have;
 set have;
 _area=input(area,ifmt.);
 format _area fmt.;
run;
proc kde data=have;
bivar _Area Quality/out=want;
freq Year_2022;
run;


proc template;
define statgraph layoutoverlay3d;
begingraph;
entrytitle "Density Plot of Height and Weight";
layout overlay3d / tilt=10 rotate=54
walldisplay=none cube=false;
surfaceplotparm x=value1 y=value2 z=density /
surfacecolorgradient=density;
endlayout;
endgraph;
end;
run;
proc sgrender data=want template=layoutoverlay3d;
format value1 fmt.;
label value1='Area' value2='Year_2022';
run;

Ksharp_0-1723174589048.png

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1292 views
  • 3 likes
  • 3 in conversation