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?
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.
Depends.. Can you be a bit more precise about the nature of your problem?
It overwrites the same variable. Is there a way to create new variable (which is recoded) in array statement
Thanks!
@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.
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
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,
@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.
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?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.