BookmarkSubscribeRSS Feed
Zaichik17
Calcite | Level 5

I have longitudinal data, with multiple obs per id so I provided only a sample of 4 id's.  I have data for many years and i only need to use year0, year 1 and year 2 so i had to create subsets. I have a variable hard_drugs(0/1) that I need to create a new variable for hard drugs that is either never, past or current ( which can be coded as 0,1,2) so if year 0, year 1 and year 2 all have hard_drug 0, then the new variable is never(0). If year 0 and/or year 1 is hard_drug 1 but year 2 is hard_drug 0, then new var is past(1). if year 2 is hard_drug is 1 then that is current(2).  I can't figure out how to do that. 

Also, I need to create a new variable that is the difference between a year2 and year 0 difference. when I try to do this, a new variable is created but the are  no values assigned and Im not sure why?

 

Here is my data:

DATA have;
INFILE Datalines dlm ="," dsd truncover;
INPUT newid AGG_MENT AGG_PHYS HASHV HASHF income BMI HBP
DIAB LIV34 KID FRP FP TCHOL TRIG LDL DYSLIP CESD SMOKE
DKGRP HEROPIATE IDU LEU3N VLOAD ADH RACE EDUCBA hivpos age
ART everART years hard_drugs;

DATALINES;
1,44.90709633,52.52556921,1,.,4,24.71755909,1,1,1,1,1,1,133,176,62,2,14,3,0,1,2,104.1659454,102013,.,1,4,1,52,0,0,0,1
1,58.20753602,41.29346502,1,4,4,26.06800717,1,1,1,1,1,1,131,107,66,1,2,3,3,1,1,262.0060502,27,2,1,4,1,53,1,1,1,0
1,59.65135974,48.54453213,1,0,4,27.16420923,1,1,1,1,1,1,180,233,86,2,1,3,0,1,1,345.4009918,60,1,1,4,1,54,1,1,2,0
1,56.806574,46.73991355,1,0,5,25.71785653,1,1,1,1,1,1,171,139,96,1,18,3,1,1,1,292.3271358,9,1,1,4,1,55,1,1,3,0
2,46.34189863,27.92331491,1,0,2,26.66936015,2,9,1,9,2,9,125,.,.,2,20,3,0,1,2,257.8277832,8121,.,3,2,1,54,0,0,0,1
2,48.71790642,38.03806704,2,0,1,25.96576195,2,9,1,9,1,9,.,.,.,9,18,3,3,1,1,459.4561691,21,1,3,2,1,55,1,1,1,0
2,45.41482533,37.32203958,1,0,2,26.96036793,2,9,1,9,1,1,134,.,.,4,18,3,2,1,1,263.0692533,48,1,3,2,1,56,1,1,2,0
2,48.05706234,31.80529414,2,0,1,27.56218329,2,1,1,2,1,1,105,104,44,1,21,3,1,1,1,238.6691021,20,1,3,2,1,60,1,1,6,0
3,40.22336832,60.06969568,1,4,6,28.59084821,9,9,1,1,1,1,170,.,.,2,18,2,0,1,2,563.1222984,4001.556158,,1,7,1,47,0,0,0,1
3,44.42011462,62.71705483,1,4,6,28.35319907,9,9,1,1,1,1,170,.,.,2,22,2,0,1,2,488.9100325,2020,1,1,7,1,48,1,1,1,1
3,41.70078566,58.51450134,2,4,6,28.18509751,1,1,1,1,1,1,180,82,127,2,23,2,0,1,2,405.1815688,27.50917363,1,1,7,1,49,1,1,2,1
3,50.03522655,51.93552598,1,0,.,26.76923019,1,9,1,2,1,1,180,.,.,9,14,2,1,1,2,366.3389365,4.219372675,2,1,7,1,51,1,1,4,1
3,55.93560718,56.14366763,2,0,7,30.4238763,1,9,2,1,1,1,162,.,.,2,1,2,2,1,2,416.9267951,6.452357495,1,1,7,1,52,1,1,5,1
4,42.9063758,50.7885018,2,4,1,20.36451325,1,1,1,9,1,1,214,97,147,2,14,3,1,2,1,110.4217933,740,.,1,5,1,44,0,0,0,1
4,31.15971215,44.62882869,1,2,1,18.21865476,1,3,1,9,2,2,197,.,.,4,25,3,2,2,1,159.6297053,26.64731734,3,1,5,1,45,1,1,1,1
4,52.68222906,51.50532947,1,0,.,20.28485484,1,1,1,9,1,1,251,260,152,2,13,3,0,1,1,179.6409126,27,2,1,5,1,46,1,1,2,0
4,50.79010982,57.02273438,.,0,3,21.1071036,1,3,1,1,1,1,234,.,.,4,9,3,1,1,1,313.4508076,29,2,1,5,1,47,1,1,3,0
4,56.31312223,32.41547791,1,0,3,18.93678664,1,1,1,1,1,1,172,155,97,2,8,3,3,1,1,367.6588994,31,2,1,5,1,48,1,1,4,0
4,44.07339059,52.80676132,1,0,3,21.60013629,1,1,1,1,1,1,251,129,173,2,6,3,1,1,1,388.0592199,35,2,1,5,1,49,1,1,5,0
;
RUN;
*Year 0;
DATA year0;
set have (DROP = ART everART HEROPIATE IDU hivpos ADH);
IF years = 0;
*Rename outcome variables*;
RENAME AGG_MENT = agg_ment0
AGG_PHYS = agg_phys0
LEU3N = leu3n0
VLOAD = vload0;
run;

TITLE 'Year 0 data';
PROC PRINT DATA = year0;
RUN;

*Year 1 only need hard drugs variable;
DATA year1;
set have (DROP = AGG_MENT AGG_PHYS HASHV HASHF income BMI HBP
DIAB LIV34 KID FRP FP TCHOL TRIG LDL DYSLIP CESD SMOKE
DKGRP HEROPIATE IDU LEU3N VLOAD ADH RACE EDUCBA hivpos age
ART everART);
IF years = 1;
run;

TITLE 'Year 1 data';
PROC PRINT DATA = year1;
RUN;

*Year 2;
DATA year2;
set have (DROP = ART everART HEROPIATE IDU hivpos);
IF years = 2;

*Rename outcome variables*;
RENAME AGG_MENT = agg_ment2
AGG_PHYS = agg_phys2
LEU3N = leu3n2
VLOAD = vload2;
RUN;
TITLE 'Year 2 data';
PROC PRINT DATA = year2;
RUN;

*now merge all together;
data allyears;
SET year0
year1
year2;
BY newid;
RUN;
proc print data =allyears;
run;


DATA allyears_clean;
SET allyears;
agg_ment_diff = agg_ment2 - agg_ment0;

agg_phys_diff =agg_phys2 - agg_phys0;

RUN;

1 REPLY 1
SASJedi
SAS Super FREQ

To combine all of the datasets horizontally, you need to use a MERGE statement instead of a SET statement:

*now merge all together;
data allyears;
   merge year0 year1 year2;
   BY newid;
RUN;
Check out my Jedi SAS Tricks for SAS Users

Ready to join fellow brilliant minds for the SAS Hackathon?

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

Register today!
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
  • 1 reply
  • 323 views
  • 0 likes
  • 2 in conversation