Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Re: Creating a new variable but can't get all values to convert

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-29-2022 02:42 PM
(484 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.