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

Hello experts,

I have survey results like the table below. For ease of analysis,  I extracted the 1st character and converted to numeric cause of the missing values, the first time I ran the code the complete table was created, the second  time there are lots of observations missing, cant seem to find out why, the log does not have an error statement.

Thanks;

 

/*Sample code*/

data sample;
set have;
new_var=substr(old_var,1,1);
run;

 

If device=  ' ' then A001=0;
else If device= 'b' then A001=1;
else If device='c' then A001=2;

label A001="device";

run;

 

Deviceactive social mediaAgeGendersatisfied with CSRpreferred time to callLocation
 b=No22a=1 e= Anytimeb=instate
b=iphonea=Yes28b=2a=Yesa=Morninga=outstate
b=iphone 35 a=Yese= Anytimea=outstate
b=iphonea=Yes35b=2a=Yese= Anytimeb=instate
a=samsunga=Yes27 a=Yese= Anytimeb=instate
a=samsunga=Yes32 a=Yesa=Morningb=instate
  37b=2  b=instate
b=iphoneb=No28a=1a=Yes b=instate
 b=No25   a=outstate
 b=No a=1 a=Morninga=outstate
  30yrs   b=instate
c=a & b   a=Yese= Anytime 
 b=No     
  23b=2 c=Afternoonb=instate
  30b=2  b=instate
a=samsungb=No  a=Yese= Anytime 
       

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

When you run the same code on the same data, you get the same result. When the result changes, you have either changed the code, or used different input data.

 

Your code as posted is incomplete (everything after the first run; is invalid).

Please use the "little running man" for posting code, and post example data for dataset "have" in a data step with datalines.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

When you run the same code on the same data, you get the same result. When the result changes, you have either changed the code, or used different input data.

 

Your code as posted is incomplete (everything after the first run; is invalid).

Please use the "little running man" for posting code, and post example data for dataset "have" in a data step with datalines.

michokwu
Quartz | Level 8

Thanks. Yes, I noticed this after editing it a little. It's the same data, I renamed some variables but followed the same coding convention that I used before. 

Apologies! this is what my code actually looks like. 

/*Sample code*/
data sample;
set have;
new_var=substr(old_var,1,1);
run;

 
data sample1;
set sample;
If device=  ' ' then A001=0;
else If device= 'a' then A001=1;
else If device= 'b' then A001=2;
else If device='c' then A001=3;
label A001="device";

keep A001;
run;
ed_sas_member
Meteorite | Level 14

Hi @michokwu 

 

It is seems that you should replace device by new_var:

 

data sample;
	set have;
	new_var=substr(old_var, 1, 1);
run;

data sample1;
	set sample;
	if new_var=' ' then A001=0;
	else if new_var='a' then A001=1;
	else if new_var='b' then A001=2;
	else if new_var='c' then A001=3;
	label A001="device";
	keep A001;
run;

Best,

michokwu
Quartz | Level 8

I changed the names of new variables back to the original names.

 

Thank you

michokwu
Quartz | Level 8

It was the code. An 'else if' statement was not specified correctly

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 941 views
  • 0 likes
  • 3 in conversation