BookmarkSubscribeRSS Feed
knargis160
Calcite | Level 5

I want to code for multiple variables from level 0 to 2 at once. what is the best wat to do in SAS rather than using if-else statements?

10 REPLIES 10
PaigeMiller
Diamond | Level 26

You could use ARRAYs. Then you need only one if-then statement. But since you give us no information about your data set variable names, we can't provide any more details.

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

Depends.. Can you be a bit more precise about the nature of your problem?

ed_sas_member
Meteorite | Level 14
Hi @knargis160
You can use an array to perform the same operation on multiple variables:

Data want;
Set have;
Array _arr(*) var1 var2 var3 ;
Do i=1 to dim(_arr);
If _arr(i)=0 then _arr(i)=2;
End;
Drop i;
Run;
knargis160
Calcite | Level 5

It overwrites the same variable. Is there a way to create new variable (which is recoded) in array statement

Thanks!

PaigeMiller
Diamond | Level 26

@knargis160 wrote:

It overwrites the same variable. Is there a way to create new variable (which is recoded) in array statement

Thanks!


Could you please explain the whole problem in sufficient detail (and by showing an example data set and by showing the desired output) that we can understand it? Then we might be able to provide code that meets your needs.

--
Paige Miller
knargis160
Calcite | Level 5

Datalines

ID P1 P2 P3 P4 ….

1   0    1    0   1

2   1     0   0   0

3   0     1     1  1

 

Output

ID P1   P2  P3 P4

1   2     1     2     1

2   1     2     2     2

3    2    1     1     1

 

I want to code all "0" to 2

As these are 15 vars like this. I don't want to use if multiple else statement

Reeza
Super User
What did you try that overwrites the variable would also help?

Your output shows as it overwriting the original values, but you said that's not what you want above so please clarify.
ed_sas_member
Meteorite | Level 14

HI @knargis160 

 

You can do this to recode variables in new variables:

data want;
	set have;
	array _arr(*) var1 var2 var3 /* Put the variable names */;
	array _new(3) ; /* Put the number of variables */
	do i=1 to dim(_arr);
		if _arr(i)=0 then _arr(i)=2;
	end;
	drop i;
run;

Best,

ballardw
Super User

@knargis160 wrote:

I want to code for multiple variables from level 0 to 2 at once. what is the best wat to do in SAS rather than using if-else statements?


Another approach can be when you read the data from an external source. (informat and example data changed from first response)

proc format library=work;
invalue changetwo
'0'=2
other=_same_
;
run;

data example;
   informat x y z changetwo.;
   input x y z;
datalines;
1 0 2
0 0 0
1 1 0;

A question though. Are you changing 0 to 2 because you have a dichotomous variable that has 1 for 'Yes' 'True' 'Present' and 0 for 'No' 'False' 'Absent' or similar? You may want to strongly consider not doing so. It can require more code to get somethings done with 1/2 coding than with 1/0 coding.

 

Consider: if you SUM a 1/0 coded variable then the total is the number of observations or variables that were coded 1.

The MEAN is the percentage of 1 values in decimal form.

Reeza
Super User

Method 1: Arrays

Here's a tutorial on using Arrays in SAS
https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

If you need further help please post example data and show what you want as output. 

Here are instructions on how to provide sample data as a data step:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...


@knargis160 wrote:

I want to code for multiple variables from level 0 to 2 at once. what is the best wat to do in SAS rather than using if-else statements?


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1389 views
  • 0 likes
  • 6 in conversation