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;
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;
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;
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.
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.