BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
StudyBeane
Calcite | Level 5

Hi, here is my predicament. I'm taking a beginner SAS course concurrently with another course that relies on the information you learned in that beginner class. I have ZERO SAS experience, only R. One of the questions is like this: 

 

"Create a new variable called “abp” that is coded 1 if the individual’s daily alcohol consumption was 80 g per day or higher, and that is coded 0 if the individual’s daily alcohol consumption was less than 80 g per day."

 

In the dataset, the acgp (original variable) is:

0 = 0-39 grams per day

1 = 40-79 grams per day

2 = 80-119 grams per day

3 = 120+ grams per day

 

So, just by scouring around youtube and other resources, I did: 

if acgp in (2,3) THEN abp = 1;
ELSE IF acgp in (0,1) THEN abp = 0;
run;

 

Now this will run but the output data is nothing, it gives me two columns with acgp and acp and no rows. The code I received was from a few basic codes given to me. I don't know SAS at all, so I don't know if I have to do anything else before doing the IF/ELSE statement? My data is a SAS data set, its already read into my program. Any advice would be VERY much appreciated!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You left out two key pieces of information needed to create a program to do what you wanted.

The NAME of the existing dataset that has the variable ACGP.

The NAME of the NEW dataset you want to create that will have the NEW variable ABP.

Let's assume those are HAVE and WANT, respectively.

 

Also your logic does not have any instructions for what to do when the value ACGP is something else, such as a missing value or an invalid code.

data want;
  set have;
  if acgp in (2,3) then abp = 1;
  else if acgp in (0,1) then abp = 0;
  else abp=.;
run;

If you are willing to have missing or invalid codes mapped to 0 then you can simplify the logic to just assigning the result of the first boolean test to ABP.  

data want;
  set have;
  abp = (acgp in (2,3));
run;

That will work because SAS evaluate boolean expressions to either 1 (TRUE) or 0 (FALSE).

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Show us the entire data step code, not a few selected lines.

 

Show us the log from running this data step (we need to see the code from this data step as it appears in the log, and any ERRORs/WARNINGs/NOTEs that appear in the log)

--
Paige Miller
Kurt_Bremser
Super User

If you did not use a SET statement to read the existing dataset, you only get these two variables with missing values in a single observation.

Please share the complete code of the step (from the DATA to the RUN statement).

Tom
Super User Tom
Super User

You left out two key pieces of information needed to create a program to do what you wanted.

The NAME of the existing dataset that has the variable ACGP.

The NAME of the NEW dataset you want to create that will have the NEW variable ABP.

Let's assume those are HAVE and WANT, respectively.

 

Also your logic does not have any instructions for what to do when the value ACGP is something else, such as a missing value or an invalid code.

data want;
  set have;
  if acgp in (2,3) then abp = 1;
  else if acgp in (0,1) then abp = 0;
  else abp=.;
run;

If you are willing to have missing or invalid codes mapped to 0 then you can simplify the logic to just assigning the result of the first boolean test to ABP.  

data want;
  set have;
  abp = (acgp in (2,3));
run;

That will work because SAS evaluate boolean expressions to either 1 (TRUE) or 0 (FALSE).

StudyBeane
Calcite | Level 5

Yes, thank you, I am extremely new to SAS and didn't realize I had to use use data/set.

ballardw
Super User

@StudyBeane wrote:

Yes, thank you, I am extremely new to SAS and didn't realize I had to use use data/set.


In R don't you have to provide some data source before creating a new variable based on existing values?

 

Pretty much the case in any of the 20+ "programming languages" I have used over the years. No input data = no meaningful output data. The principles wont change but approach will change based on the language used.  Such  as how to point to the existing data: Set statement for a SAS data set or DATA= for almost every other SAS procedure, and where to put the output: Data statement with the Data step code. Other approaches in  other procedures but OUTPUT or OUT= are fairly common but the syntax can vary based on the procedure.

 

SAS will default to the last created data set as input for many procedures but the Data step still needs a SET statement to do such even if no data set name is provided.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 5 replies
  • 466 views
  • 0 likes
  • 5 in conversation