BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ayosas
Fluorite | Level 6

Hello

 

I am working with a large dataset and trying to subset the data to only keep certain variables, I am only looking at a certain age range for the data too so I want to delete the ages that I do not need. My code for this isn't working and I'm not sure why. 

 

data nsch1718;
if  2=>SC_AGE_YEARS>5 then delete;
keep ACEct_1718 PrntNativity_1718 TalkAbout_1718 WrkToSolve_1718 strengths_1718 hopeful_1718 CtFamRes_1718 FamResilience_1718 
confident_1718 ACEdivorce_1718 ACEdeath_1718 ACEjail_1718 ACEdomviol_1718 ACEneighviol_1718 ACEmhealth_1718 ACEdrug_1718 ACEdiscrim_1718
sex_1718 SC_AGE_YEARS SC_HISPANIC_R SC_RACE_R raceAsia_1718 povSCHIP_1718 FPL_I1 AdultEduc_1718 FamCount_1718 RecogLetter_1718 
CountTo_1718 CONFIDENT WorkToFin_1718 HurtSad_1718 WriteName_1718;
format CONFIDENT confidentf. prntnativity_1718 prntnativityf. confident_1718 confidentf.;
set mc759.nsch1718combined_drc;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

This instead

 

data nsch1718;
set mc759.nsch1718combined_drc;
if 2 < SC_AGE_YEARS <= 5;
keep ACEct_1718 PrntNativity_1718 TalkAbout_1718 WrkToSolve_1718 strengths_1718 hopeful_1718 CtFamRes_1718 FamResilience_1718 
confident_1718 ACEdivorce_1718 ACEdeath_1718 ACEjail_1718 ACEdomviol_1718 ACEneighviol_1718 ACEmhealth_1718 ACEdrug_1718 ACEdiscrim_1718
sex_1718 SC_AGE_YEARS SC_HISPANIC_R SC_RACE_R raceAsia_1718 povSCHIP_1718 FPL_I1 AdultEduc_1718 FamCount_1718 RecogLetter_1718 
CountTo_1718 CONFIDENT WorkToFin_1718 HurtSad_1718 WriteName_1718;
format CONFIDENT confidentf. prntnativity_1718 prntnativityf. confident_1718 confidentf.;
run;
PG

View solution in original post

4 REPLIES 4
Reeza
Super User

Because your SET statement is after all your code. It needs to be BEFORE the logic you want to execute. Otherwise you're referencing values that don't exist yet.


data nsch1718;
set mc759.nsch1718combined_drc;
if 2=>SC_AGE_YEARS>5 then delete;
keep ACEct_1718 PrntNativity_1718 TalkAbout_1718 WrkToSolve_1718 strengths_1718 hopeful_1718 CtFamRes_1718 FamResilience_1718 confident_1718 ACEdivorce_1718 ACEdeath_1718 ACEjail_1718 ACEdomviol_1718 ACEneighviol_1718 ACEmhealth_1718 ACEdrug_1718 ACEdiscrim_1718 sex_1718 SC_AGE_YEARS SC_HISPANIC_R SC_RACE_R raceAsia_1718 povSCHIP_1718 FPL_I1 AdultEduc_1718 FamCount_1718 RecogLetter_1718 CountTo_1718 CONFIDENT WorkToFin_1718 HurtSad_1718 WriteName_1718;
format CONFIDENT confidentf. prntnativity_1718 prntnativityf. confident_1718 confidentf.; run;



If this doesn't work, post the log from the code.

 

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

 


@ayosas wrote:

Hello

 

I am working with a large dataset and trying to subset the data to only keep certain variables, I am only looking at a certain age range for the data too so I want to delete the ages that I do not need. My code for this isn't working and I'm not sure why. 

 

data nsch1718;
if  2=>SC_AGE_YEARS>5 then delete;
keep ACEct_1718 PrntNativity_1718 TalkAbout_1718 WrkToSolve_1718 strengths_1718 hopeful_1718 CtFamRes_1718 FamResilience_1718 
confident_1718 ACEdivorce_1718 ACEdeath_1718 ACEjail_1718 ACEdomviol_1718 ACEneighviol_1718 ACEmhealth_1718 ACEdrug_1718 ACEdiscrim_1718
sex_1718 SC_AGE_YEARS SC_HISPANIC_R SC_RACE_R raceAsia_1718 povSCHIP_1718 FPL_I1 AdultEduc_1718 FamCount_1718 RecogLetter_1718 
CountTo_1718 CONFIDENT WorkToFin_1718 HurtSad_1718 WriteName_1718;
format CONFIDENT confidentf. prntnativity_1718 prntnativityf. confident_1718 confidentf.;
set mc759.nsch1718combined_drc;
run;

 

ballardw
Super User

The "if" has to come after the SET. Otherwise there are no values to compare.

 

Please describe the ages that you want to keep.

When you say Age > 5 then the 2>= age will never be true so the compound comparison will not find any records. (2 not being larger than 5)

 

If you want to keep records with the age between 2 and 5 this is more likely.

 

If 2 <= age <=5 ;

 

An If of this form is called a subsetting if and only records with the comparison true are kept.

 

PGStats
Opal | Level 21

This instead

 

data nsch1718;
set mc759.nsch1718combined_drc;
if 2 < SC_AGE_YEARS <= 5;
keep ACEct_1718 PrntNativity_1718 TalkAbout_1718 WrkToSolve_1718 strengths_1718 hopeful_1718 CtFamRes_1718 FamResilience_1718 
confident_1718 ACEdivorce_1718 ACEdeath_1718 ACEjail_1718 ACEdomviol_1718 ACEneighviol_1718 ACEmhealth_1718 ACEdrug_1718 ACEdiscrim_1718
sex_1718 SC_AGE_YEARS SC_HISPANIC_R SC_RACE_R raceAsia_1718 povSCHIP_1718 FPL_I1 AdultEduc_1718 FamCount_1718 RecogLetter_1718 
CountTo_1718 CONFIDENT WorkToFin_1718 HurtSad_1718 WriteName_1718;
format CONFIDENT confidentf. prntnativity_1718 prntnativityf. confident_1718 confidentf.;
run;
PG
ayosas
Fluorite | Level 6
Thank you!!

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 883 views
  • 2 likes
  • 4 in conversation