BookmarkSubscribeRSS Feed
Corinthian94
Obsidian | Level 7

Hi there,

 

I'm trying to make a new variable that will convert a variable to a new variable representing packs of cigarettes smoked per week and am having some issues. I have run this code several times and checked the log and it has no errors, but for some reason when I run a proc freq on it it only gives shows me the values for values that were already in cigarettes/week values. 

 

Some context. The original variables (q6_4 and q6_10)  were coded as follows:

 

The first digit of the answer identifies the unit (1 represent day, 2 represent week, etc.), the next two digits was the number smoked during that unit of time frame. This is coded as a categorical variable.

 

_ _ _      Enter number of cigarettes                          1 pack = 20 cigarettes

 

101 ‑ 199            Number per day

201 ‑ 299            Number per week

301 ‑ 399            Number per month

401 ‑ 499            Number per year

888                                    Don't Know/Not Sure

999                                    Refused

 

What I tried to do is convert them to numeric, and then use that numerical variable to calculate packs smoked per week for all the individuals who smoke any amount of cigarettes. Calculations are as follows

 

Convert cigarettes/day to cigarettes/week: (cigarettes/day)*7

Convert cigarettes/month to cigarettes/week: (cigarettes/month)/4.345 (4.345 weeks in a month on average)

Convert cigarettes/year to cigarettes/week: (cigarettes/year)/52

 

The code I am using is below:

 

*Convert smoking variables to numeric;

if q6_4 ne "" and smoke_stat = 1 then numerical_smoke = input(q6_4, 4.);
else if q6_10 ne "" and smoke_stat = 1 then numerical_smoke = input(q6_10, 4.);
else numerical_smoke = 0;

 

*Make variable for current packs per week at baseline;

if numerical_smoke gt 100 and numerical_smoke lt 200 then cig_perweek = ((numerical_smoke - 100)*7);
else if numerical_smoke gt 200 and numerical_smoke lt 300 then cig_perweek = (numerical_smoke - 200);
else if numerical_smoke gt 300 and numerical_smoke lt 400 then cig_perweek = ((numerical_smoke - 300)/4.345);
else if numerical_smoke gt 400 and numerical_smoke lt 500 then cig_perweek = ((numerical_smoke - 400)/52);
else cig_perweek = 0;

 

*Create pack per week variable;

pack_perweek = cig_perweek/20;

 

I honestly don't know why it won't show me the full range of values I've asked for. Any ideas?

 

Thanks!

 

 

5 REPLIES 5
Tom
Super User Tom
Super User

Are you sure the original variable is character?  And if it is character are the digits really in the first 4 bytes of the variable, that is there are no leading spaces? If the answer is not YES to BOTH questions then your INPUT() function is not going to work.

Corinthian94
Obsidian | Level 7

Yes, I'm positive the original variable is character, although I'm not sure why it is coded this way. I ran a proc freq and proc means on the new numeric variable and it worked so I don't think the 4 bytes is the issue either. Any other ideas?

Tom
Super User Tom
Super User

Try running the PROC FREQ and use the $QUOTE format on the variable and see what you get.  ODS outputs like HTML/RTF/PDF will "eat" the leading spaces.

 

Do the values actually include the hyphens in your description?

Or just individual 3 digit strings?

 

Break your process into steps and see what you get.

data want;
  set have;
  first_digit = char(q6_4,1);
  rest=substr(q6_4,2);
run;

proc freq data=want;
  tables first_digit*rest*q6_4 / list missing;
  format q6_4 $quote.;
run;
ballardw
Super User

Provide some input values and the expected value along with the incorrect output.

 

Your very infrequent smokers, such as smoke 1 cigarette per year are going to have "pack" in the 0.001 range after rounding.

 

 

 

Corinthian94
Obsidian | Level 7

Thank you for your help! I ended up figuring it out changing the order of my statements. Appreciate your time!

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!

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.

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