<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to create a column which is market value of last month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927017#M364848</link>
    <description>&lt;P&gt;&lt;SPAN&gt;the FIRMS market value everyday BASED ON THEIR&amp;nbsp;MARKET VALUE by&amp;nbsp;PREVIOUS MONTH’S end.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for the first month, the market value would be everyday's market value.&lt;/P&gt;
&lt;P&gt;But after the second month, the base market value(LMV) every day should be the same as the MV of&amp;nbsp;&lt;SPAN&gt;PREVIOUS MONTH’S end (not change everyday, but same as end of last month).&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sat, 04 May 2024 13:52:36 GMT</pubDate>
    <dc:creator>Irenelee</dc:creator>
    <dc:date>2024-05-04T13:52:36Z</dc:date>
    <item>
      <title>How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926993#M364832</link>
      <description>&lt;P&gt;thx!&lt;/P&gt;
&lt;P&gt;How to create a column which is market value of last month&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DM'LOG; CLEAR; OUT; CLEAR; ODSRESULTS; CLEAR;';
%LET FOLDER=%STR(C:\USERS\ALAIN\ONEDRIVE\桌面\HIGH);
LIBNAME HIGH "&amp;amp;FOLDER";

DATA HH;
 SET HIGH.HH;
 IF CFACPR=0 THEN DELETE;
RUN;

DATA HH;
	SET	HH;
	IF _N_ &amp;lt;=1000;
RUN;

PROC SORT 
  DATA=HH (KEEP  =SHRCD EXCHCD PERMNO DATE PRC CFACPR SHROUT CFACSHR
                      WHERE =( SHRCD IN (10,11) AND EXCHCD IN (1,2,3,31,32,33) )
                      ) 

OUT=HL(DROP=SHRCD EXCHCD );
  BY PERMNO DATE;
RUN;

DATA HL; 
    SET HL; 
    BY PERMNO DATE; 
    RETAIN H L 0; 
    P=ABS(DIVIDE (PRC, CFACPR));
    IF FIRST.PERMNO THEN H = P; 
    IF FIRST.PERMNO THEN L = P;
    H = MAX(H, P); 
    L = MIN(L, P);
 DROP  PRC CFACPR;
   RUN; /* PERMNO DATE  SHROUT CFACSHR H L P */

data MK (drop=_:);
  set HL;
  BY PERMNO date; 

  retain _first_date_qualified . 
         _H   .                 
         _H_date .              
         _L   .                 
         _L_date .              
          PORTFOLIO 
;             
LENGTH  PORTFOLIO $11;

  if first.permno then do;
    call missing(of _:);
    _first_date_qualified=intnx('year',date,1,'same')+1;
  end;

  if _H=. then _H=P;
  if _L=. then _L=P;

  if P ^=. then do;
    if P &amp;gt; _H and date &amp;gt;= INTNX('MONTH', _H_DATE, 1, 'SAME') then do;
      _H=P;
      _H_date=date;
      portfolio='Max';
    end;

    else if P &amp;lt; _L and date &amp;gt;= INTNX('MONTH', _L_DATE, 1, 'SAME') then do;
      _L=P;
      _L_date=date;
      portfolio='Min';
    end;

    else portfolio='Comparison';
  end;

  if date &amp;gt;= _first_date_qualified then do;
      H = _H;
      H_date = _H_date;
      L = _L;
      L_date = _L_date;
  end;

  if date &amp;lt; _first_date_qualified then do;
    portfolio = 'Unqualified';
  end;

  format H_date L_date yymmdd10. ;

run;

DATA MV;
 SET MK;
 BY PERMNO date; 

 Retain 
 MV
 Last_Month_End; 

 Last_Month_End = INTNX('MONTH', DATE, -1, 'end');
 format Last_Month_End yymmdd10. ;

 MV = P * (SHROUT*CFACSHR) ;

RUN;

DATA LMV; 
    SET MV; 
    BY PERMNO DATE; 
    RETAIN 
    LMV 0; 
    IF FIRST.PERMNO THEN LMV = MV; 
    IF LAG(DATE)=Last_Month_End  THEN LMV = LAG(MV);
	ELSE LMV = LAG(MV);
   RUN; 


DATA L MV;
  SET LMV(OBS=500);
  PUT (_ALL_) (+0);
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's not as expected now..&lt;/P&gt;
&lt;P&gt;10000 1986-01-07 3680 1 2.5625 2.5625 2.5625 Unqualified . . 9430 1985-12-31 .&lt;BR /&gt;10000 1986-01-08 3680 1 2.5625 2.5 2.5 Unqualified . . 9200 1985-12-31 9430&lt;BR /&gt;10000 1986-01-09 3680 1 2.5625 2.5 2.5 Unqualified . . 9200 1985-12-31 9200&lt;BR /&gt;10000 1986-01-10 3680 1 2.5625 2.5 2.5 Unqualified . . 9200 1985-12-31 9200&lt;BR /&gt;10000 1986-01-13 3680 1 2.625 2.5 2.625 Unqualified . . 9660 1985-12-31 9200&lt;BR /&gt;10000 1986-01-14 3680 1 2.75 2.5 2.75 Unqualified . . 10120 1985-12-31 9660&lt;BR /&gt;10000 1986-01-15 3680 1 2.875 2.5 2.875 Unqualified . . 10580 1985-12-31 10120&lt;BR /&gt;10000 1986-01-16 3680 1 3 2.5 3 Unqualified . . 11040 1985-12-31 10580&lt;BR /&gt;10000 1986-01-17 3680 1 3 2.5 3 Unqualified . . 11040 1985-12-31 11040&lt;BR /&gt;10000 1986-01-20 3680 1 3 2.5 3 Unqualified . . 11040 1985-12-31 11040&lt;BR /&gt;10000 1986-01-21 3680 1 3 2.5 3 Unqualified . . 11040 1985-12-31 11040&lt;BR /&gt;10000 1986-01-22 3680 1 3 2.5 3 Unqualified . . 11040 1985-12-31 11040&lt;BR /&gt;10000 1986-01-23 3680 1 3.75 2.5 3.75 Unqualified . . 13800 1985-12-31 11040&lt;BR /&gt;10000 1986-01-24 3680 1 4.1875 2.5 4.1875 Unqualified . . 15410 1985-12-31 13800&lt;BR /&gt;10000 1986-01-27 3680 1 4.4375 2.5 4.4375 Unqualified . . 16330 1985-12-31 15410&lt;BR /&gt;10000 1986-01-28 3680 1 4.4375 2.5 4.4375 Unqualified . . 16330 1985-12-31 16330&lt;BR /&gt;10000 1986-01-29 3680 1 4.4375 2.5 4.3125 Unqualified . . 15870 1985-12-31 16330&lt;BR /&gt;10000 1986-01-30 3680 1 4.4375 2.5 4.4375 Unqualified . . 16330 1985-12-31 15870&lt;BR /&gt;10000 1986-01-31 3680 1 4.4375 2.5 4.375 Unqualified . . 16100 1985-12-31 16330&lt;BR /&gt;10000 1986-02-03 3680 1 4.4375 2.5 4.375 Unqualified . . 16100 1986-01-31 .&lt;BR /&gt;10000 1986-02-04 3680 1 4.4375 2.5 4.375 Unqualified . . 16100 1986-01-31 16100&lt;BR /&gt;10000 1986-02-05 3680 1 4.4375 2.5 4.375 Unqualified . . 16100 1986-01-31 16100&lt;BR /&gt;10000 1986-02-06 3680 1 4.4375 2.5 4.1875 Unqualified . . 15410 1986-01-31 16100&lt;BR /&gt;10000 1986-02-07 3680 1 4.4375 2.5 4.375 Unqualified . . 16100 1986-01-31 15410&lt;BR /&gt;10000 1986-02-10 3680 1 4.4375 2.5 4.3125 Unqualified . . 15870 1986-01-31 16100&lt;BR /&gt;10000 1986-02-11 3680 1 4.4375 2.5 4.3125 Unqualified . . 15870 1986-01-31 15870&lt;BR /&gt;10000 1986-02-12 3680 1 4.4375 2.5 4.21875 Unqualified . . 15525 1986-01-31 15870&lt;BR /&gt;10000 1986-02-13 3680 1 4.4375 2.5 4.21875 Unqualified . . 15525 1986-01-31 15525&lt;BR /&gt;10000 1986-02-14 3680 1 4.4375 2.5 4.28125 Unqualified . . 15755 1986-01-31 15525&lt;BR /&gt;10000 1986-02-18 3680 1 4.4375 2.5 4 Unqualified . . 14720 1986-01-31 15755&lt;BR /&gt;10000 1986-02-19 3680 1 4.4375 2.5 3.9375 Unqualified . . 14490 1986-01-31 14720&lt;BR /&gt;10000 1986-02-20 3680 1 4.4375 2.5 3.6875 Unqualified . . 13570 1986-01-31 14490&lt;BR /&gt;10000 1986-02-21 3680 1 4.4375 2.5 3.6875 Unqualified . . 13570 1986-01-31 13570&lt;BR /&gt;10000 1986-02-24 3680 1 4.4375 2.5 3.625 Unqualified . . 13340 1986-01-31 13570&lt;BR /&gt;10000 1986-02-25 3680 1 4.4375 2.5 3.5625 Unqualified . . 13110 1986-01-31 13340&lt;BR /&gt;10000 1986-02-26 3680 1 4.4375 2.5 3.25 Unqualified . . 11960 1986-01-31 13110&lt;BR /&gt;10000 1986-02-27 3680 1 4.4375 2.5 3.25 Unqualified . . 11960 1986-01-31 11960&lt;BR /&gt;10000 1986-02-28 3680 1 4.4375 2.5 3.25 Unqualified . . 11960 1986-01-31 11960&lt;BR /&gt;10000 1986-03-03 3680 1 4.4375 2.5 3.25 Unqualified . . 11960 1986-02-28 16100&lt;BR /&gt;10000 1986-03-04 3680 1 4.4375 2.5 3.4375 Unqualified . . 12650 1986-02-28 11960&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A title="DATA" href="https://drive.google.com/file/d/1grx_6p-yVMVzrzvVjdtfwyFUjRpq10W6/view?usp=sharing" target="_self"&gt;https://drive.google.com/file/d/1grx_6p-yVMVzrzvVjdtfwyFUjRpq10W6/view?usp=sharing&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 11:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926993#M364832</guid>
      <dc:creator>Irenelee</dc:creator>
      <dc:date>2024-05-04T11:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926994#M364833</link>
      <description>&lt;P&gt;Provide further explanation. What is wrong, specifically, and what would you like to see instead.&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 11:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926994#M364833</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-05-04T11:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926995#M364834</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I want the FIRMS market value everyday BASED ON THEIR&amp;nbsp;&lt;SPAN&gt;MARKET VALUE by&amp;nbsp;&lt;/SPAN&gt;PREVIOUS MONTH’S end.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for the first month, the market value would be everyday's market value.&lt;/P&gt;
&lt;P&gt;But after the second month, the base market value(LMV) every day should be the same as the MV of&amp;nbsp;&lt;SPAN&gt;PREVIOUS MONTH’S end (not change everyday, but same as end of last month).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 11:48:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926995#M364834</guid>
      <dc:creator>Irenelee</dc:creator>
      <dc:date>2024-05-04T11:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926999#M364835</link>
      <description>&lt;P&gt;the following is not as expected&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="LMV.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96175iF681CB7FD2043A54/image-size/large?v=v2&amp;amp;px=999" role="button" title="LMV.png" alt="LMV.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 12:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/926999#M364835</guid>
      <dc:creator>Irenelee</dc:creator>
      <dc:date>2024-05-04T12:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927010#M364843</link>
      <description>&lt;P&gt;So, what is expected?&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 13:12:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927010#M364843</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-04T13:12:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927014#M364846</link>
      <description>&lt;P&gt;Can you just simplify to the question you are asking?&lt;/P&gt;
&lt;P&gt;So let's assume you have dataset with some type of ID variable (looks like perhaps&amp;nbsp;PERMNO is that variable in your case?) some type of DATE variable and some type of numeric VALUE variable.&amp;nbsp; You want to make a NEW variable that has the value from the previous end of month.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So use the LAG() function to get the previous OBSERVATION's value.&amp;nbsp; Then when you are at the first date of a new month save that lagged value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by permno date ;
  lag_value = lag(value);
  if first.permno then call missing(lag_value,previous_value);
  else if month(date) ne month(lag(date)) then previous_value=lag_value;
  retain previous_value;
  drop lag_value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 13:47:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927014#M364846</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-05-04T13:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927017#M364848</link>
      <description>&lt;P&gt;&lt;SPAN&gt;the FIRMS market value everyday BASED ON THEIR&amp;nbsp;MARKET VALUE by&amp;nbsp;PREVIOUS MONTH’S end.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for the first month, the market value would be everyday's market value.&lt;/P&gt;
&lt;P&gt;But after the second month, the base market value(LMV) every day should be the same as the MV of&amp;nbsp;&lt;SPAN&gt;PREVIOUS MONTH’S end (not change everyday, but same as end of last month).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 13:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927017#M364848</guid>
      <dc:creator>Irenelee</dc:creator>
      <dc:date>2024-05-04T13:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927018#M364849</link>
      <description>&lt;P&gt;Yes! You are so brilliant!!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;10000 1986-01-07 3680 1 2.5625 2.5625 2.5625 UNQUALIFIED . . 9430 1985-12-31 .&lt;BR /&gt;10000 1986-01-08 3680 1 2.5625 2.5 2.5 UNQUALIFIED . . 9200 1985-12-31 9430&lt;BR /&gt;10000 1986-01-09 3680 1 2.5625 2.5 2.5 UNQUALIFIED . . 9200 1985-12-31 9430&lt;BR /&gt;10000 1986-01-10 3680 1 2.5625 2.5 2.5 UNQUALIFIED . . 9200 1985-12-31 9430&lt;BR /&gt;10000 1986-01-13 3680 1 2.625 2.5 2.625 UNQUALIFIED . . 9660 1985-12-31 9430&lt;BR /&gt;10000 1986-01-14 3680 1 2.75 2.5 2.75 UNQUALIFIED . . 10120 1985-12-31 9430&lt;BR /&gt;10000 1986-01-15 3680 1 2.875 2.5 2.875 UNQUALIFIED . . 10580 1985-12-31 9430&lt;BR /&gt;10000 1986-01-16 3680 1 3 2.5 3 UNQUALIFIED . . 11040 1985-12-31 9430&lt;BR /&gt;10000 1986-01-17 3680 1 3 2.5 3 UNQUALIFIED . . 11040 1985-12-31 9430&lt;BR /&gt;10000 1986-01-20 3680 1 3 2.5 3 UNQUALIFIED . . 11040 1985-12-31 9430&lt;BR /&gt;10000 1986-01-21 3680 1 3 2.5 3 UNQUALIFIED . . 11040 1985-12-31 9430&lt;BR /&gt;10000 1986-01-22 3680 1 3 2.5 3 UNQUALIFIED . . 11040 1985-12-31 9430&lt;BR /&gt;10000 1986-01-23 3680 1 3.75 2.5 3.75 UNQUALIFIED . . 13800 1985-12-31 9430&lt;BR /&gt;10000 1986-01-24 3680 1 4.1875 2.5 4.1875 UNQUALIFIED . . 15410 1985-12-31 9430&lt;BR /&gt;10000 1986-01-27 3680 1 4.4375 2.5 4.4375 UNQUALIFIED . . 16330 1985-12-31 9430&lt;BR /&gt;10000 1986-01-28 3680 1 4.4375 2.5 4.4375 UNQUALIFIED . . 16330 1985-12-31 9430&lt;BR /&gt;10000 1986-01-29 3680 1 4.4375 2.5 4.3125 UNQUALIFIED . . 15870 1985-12-31 9430&lt;BR /&gt;10000 1986-01-30 3680 1 4.4375 2.5 4.4375 UNQUALIFIED . . 16330 1985-12-31 9430&lt;BR /&gt;10000 1986-01-31 3680 1 4.4375 2.5 4.375 UNQUALIFIED . . 16100 1985-12-31 9430&lt;BR /&gt;10000 1986-02-03 3680 1 4.4375 2.5 4.375 UNQUALIFIED . . 16100 1986-01-31 16100&lt;BR /&gt;10000 1986-02-04 3680 1 4.4375 2.5 4.375 UNQUALIFIED . . 16100 1986-01-31 16100&lt;BR /&gt;10000 1986-02-05 3680 1 4.4375 2.5 4.375 UNQUALIFIED . . 16100 1986-01-31 16100&lt;BR /&gt;10000 1986-02-06 3680 1 4.4375 2.5 4.1875 UNQUALIFIED . . 15410 1986-01-31 16100&lt;BR /&gt;10000 1986-02-07 3680 1 4.4375 2.5 4.375 UNQUALIFIED . . 16100 1986-01-31 16100&lt;BR /&gt;10000 1986-02-10 3680 1 4.4375 2.5 4.3125 UNQUALIFIED . . 15870 1986-01-31 16100&lt;BR /&gt;10000 1986-02-11 3680 1 4.4375 2.5 4.3125 UNQUALIFIED . . 15870 1986-01-31 16100&lt;BR /&gt;10000 1986-02-12 3680 1 4.4375 2.5 4.21875 UNQUALIFIED . . 15525 1986-01-31 16100&lt;BR /&gt;10000 1986-02-13 3680 1 4.4375 2.5 4.21875 UNQUALIFIED . . 15525 1986-01-31 16100&lt;BR /&gt;10000 1986-02-14 3680 1 4.4375 2.5 4.28125 UNQUALIFIED . . 15755 1986-01-31 16100&lt;BR /&gt;10000 1986-02-18 3680 1 4.4375 2.5 4 UNQUALIFIED . . 14720 1986-01-31 16100&lt;BR /&gt;10000 1986-02-19 3680 1 4.4375 2.5 3.9375 UNQUALIFIED . . 14490 1986-01-31 16100&lt;BR /&gt;10000 1986-02-20 3680 1 4.4375 2.5 3.6875 UNQUALIFIED . . 13570 1986-01-31 16100&lt;BR /&gt;10000 1986-02-21 3680 1 4.4375 2.5 3.6875 UNQUALIFIED . . 13570 1986-01-31 16100&lt;BR /&gt;10000 1986-02-24 3680 1 4.4375 2.5 3.625 UNQUALIFIED . . 13340 1986-01-31 16100&lt;BR /&gt;10000 1986-02-25 3680 1 4.4375 2.5 3.5625 UNQUALIFIED . . 13110 1986-01-31 16100&lt;BR /&gt;10000 1986-02-26 3680 1 4.4375 2.5 3.25 UNQUALIFIED . . 11960 1986-01-31 16100&lt;BR /&gt;10000 1986-02-27 3680 1 4.4375 2.5 3.25 UNQUALIFIED . . 11960 1986-01-31 16100&lt;BR /&gt;10000 1986-02-28 3680 1 4.4375 2.5 3.25 UNQUALIFIED . . 11960 1986-01-31 16100&lt;BR /&gt;10000 1986-03-03 3680 1 4.4375 2.5 3.25 UNQUALIFIED . . 11960 1986-02-28 11960&lt;BR /&gt;10000 1986-03-04 3680 1 4.4375 2.5 3.4375 UNQUALIFIED . . 12650 1986-02-28 11960&lt;BR /&gt;10000 1986-03-05 3680 1 4.4375 2.5 3.46875 UNQUALIFIED . . 12765 1986-02-28 11960&lt;BR /&gt;10000 1986-03-06 3680 1 4.4375 2.5 3.875 UNQUALIFIED . . 14260 1986-02-28 11960&lt;BR /&gt;10000 1986-03-07 3680 1 4.4375 2.5 4.4375 UNQUALIFIED . . 16330 1986-02-28 11960&lt;BR /&gt;10000 1986-03-10 3680 1 4.4375 2.5 4.1875 UNQUALIFIED . . 15410 1986-02-28 11960&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create&amp;nbsp;a column "LMV" for each PERMNO everyday which is its market value of end of last month.(make a NEW variable that has the value from the previous end of month. )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have dataset with some type of ID variable (PERMNO is the variable in my case)&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 14:04:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927018#M364849</guid>
      <dc:creator>Irenelee</dc:creator>
      <dc:date>2024-05-04T14:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to create a column which is market value of last month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927019#M364850</link>
      <description>&lt;P&gt;For each permno you want LMV, defined as&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;current daily MV for the first month&lt;/LI&gt;
&lt;LI&gt;for all other month, the MV of the last trading date of the prior month.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;This should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA LMV (drop=_:  nxt_:) ;
    SET MV (keep=permno);
    BY PERMNO; 
    merge mv 
          mv (firstobs=2 keep=date rename=(date=nxt_date)) ;

    RETAIN LMV .; 

    if first.permno then _monthnum=1;
    if _monthnum=1 then lmv=mv;  
    output;

    /* if this is the last trading date of the month, prepare for next month */
    if intnx('month',date,nxt_date)&amp;gt;0 then do;
      _monthnum+1;
      lmv=mv;
    end;
RUN; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The initial code (below) would consistently, just for the last date of each month, mistakenly use the current MV for the LMV.&amp;nbsp; That has to be delayed by one date, as above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;STRIKE&gt;DATA LMV (drop=_:  nxt_:) ;
    SET MV (keep=permno);
    BY PERMNO; 
    merge mv 
          mv (firstobs=2 keep=date rename=(date=nxt_date)) ;

    RETAIN LMV 0; 

    if first.permno then _monthnum=1;
    if _monthnum=1 or intck('month',date,nxt_date)&amp;gt;0 then lmv=mv;  /* If first month, or end-of-current month*/

    if intnx('month',lag(date),date)&amp;gt;0 then _monthnum+1;  /*Starting a new month */
RUN;&lt;/STRIKE&gt; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2024 22:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-a-column-which-is-market-value-of-last-month/m-p/927019#M364850</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-05-04T22:12:32Z</dc:date>
    </item>
  </channel>
</rss>

