New SAS User

Completely new to SAS or trying something new with SAS? Post here for help getting started.
BookmarkSubscribeRSS Feed
Tobiasp94
Calcite | Level 5

Skærmbillede 2019-02-25 kl. 22.48.40.png

I need help changing my year variable into a dummy variable, so i can run this macro. 
The variable year variable is currently a numeric variable (2010-2017). 

Thank you

18 REPLIES 18
Astounding
PROC Star

Most likely, you will end up using YEAR as is.  Analytical procedures will normally let you use this statement:

 

class year;

 

That will create dummy variables as needed.

 

If you really have to have a dummy variable, one dummy variable is not enough.  You have 8 values for YEAR.  You will need 7 dummy variables (rarely 8, depending on your intentions).  Does that fit your plan?

Tobiasp94
Calcite | Level 5
How do I use the class statement in the macro?
If i have to create a dumme wouldn´t it be
2010=0
2011=1
........
2017= 8 ?
Astounding
PROC Star

Assuming you successfully did something along these lines:

 

2010=0

2011=1

2012=2

...

2017=7

 

You would be no better off than if you had done nothing.  The original variable YEAR with values 2010-2017 could have been used any place these new values could be used.

 

If you want advice about using a set of values within a macro, you might need to post the macro.

Reeza
Super User

Here's one way, data step methods:

 


*declare an array;
array _years(2012:2017) year2012-year2017;

*set all values to 0 initially;
do i=lbound(_years) to hbound(_years);
    _years(i) = 0;
end;

*assign year to 1;
_years(year) = 1;



and here's another, more automated:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-dummy-variables-Categorical-Var...

 

PS please post your code as text not as an image. An image means if we wanted to modify or include your code we have to type it out. 

 


@Tobiasp94 wrote:

Skærmbillede 2019-02-25 kl. 22.48.40.png

I need help changing my year variable into a dummy variable, so i can run this macro. 
The variable year variable is currently a numeric variable (2010-2017). 

Thank you


 

Tobiasp94
Calcite | Level 5
I can se that this creates a dummies.
But i can´t get it to work in my model.
The idea is that year will take a value of time=1 time=2 time=3 etc.

Thank you for your quick response

%let yvar=COPD;

%let xvars=Healthexpenditure Share_18_66 Share67_ Healthcosts Healthcosts_cofinancing Costelderlyadults Incomefromtaxes Costsports Socioeconomicindex Vocationaleducation Furthereducation Westerns Nonwesterns Dailysmokers Heavysmokers Physicalactivity Unhealthydiet Alcohol Familycontact;

%let wset=wnykom94;

%let datset=final;

%let region=Kkode;

%let time=year;

libname wcat '/folders/myfolders/sasuser.v94';

data set1; set work.&datset;
data sety; set set1; by year;if first.year;
yearch=put(year,4.);

proc iml;
print 'SUR model, assuming equal coefficients across time periods',,;
use set1;
yname={&yvar};
read all var yname into y;
xname={&xvars};
incxname={'Constant'}||xname;
read all var xname into x0;
use sety;
read all var {'year'} into year;
read all var {'yearch'} into yearch;
PaigeMiller
Diamond | Level 26

You are doing some type of modeling in IML.

 

Unless it is a modeling technique not available elsewhere in SAS, don't do it in IML. Save yourself a lot of trouble, use the modeling PROCs that SAS has built and debugged, most of which use the CLASS statement so you don't have to create your own dummy variables.

--
Paige Miller
Tobiasp94
Calcite | Level 5
How would you suggest me to use the class statement. I have created a balanced dataset before i created the macro. I could easily use the class statement in to create a new data sheet before i run the macro with new values?
I would like if you could give me the specific kode since im not use to work in SAS.
Tobiasp94
Calcite | Level 5
It seems like this code has changed my entire dataset. How do i convert it back to numeric. You suggested that the variable should stay numeric through the regression
Reeza
Super User
What code and/or where? You never mentioned IML initially so my code isn't IML. Just remove that section and you need to create it using IML functions. Unfortunately I'm not familiar with IML.
Astounding
PROC Star
Not familiar with IML, so here is an educated guess. If you create a new variable in this way:

Time = year - 2010;

Couldn't you just use Time instead of Year within IML?
Tobiasp94
Calcite | Level 5
Im really not that experienced in SAS, so i have difficulties creating a new variable and implementing it into my macro.
Astounding
PROC Star
In that case you are probably best off removing all the macro language. Any place you see an ampersand, get rid of it. For example, replace &yvar with an actual variable name. That way, you stand a fighting chance of understanding your final program.
Tobiasp94
Calcite | Level 5
Im sorry i can´t do that. I have an assignment due to tomorrow and there is no way i can get it done without the macro.
What if a implemented the class year statement in my dataset before i implemente the dataset in my macro?
What would the specific code be then?
Reeza
Super User
Have you tried using YEAR just as is? Has that caused an issue? You haven't posted any code so far that seems to require dummy variables, so it's hard to understand what you need here. Or why.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 18 replies
  • 2451 views
  • 2 likes
  • 4 in conversation